Tuples in Python are read only lists which means after a tuple is created it does not allow any of its elements to be modified. In this post we are going to see that tuple elements can be modified. Following are the examples.
From the above example it is evident that we are able to update tuple with new values. This is possible only when elements in tuple are mutable. That’s why when we add a list [10,20] in first case, dictionary {‘a’: 100, ‘b’: 200} in second case and set {88, 99} in third case we are able to add elements to them because they are mutable data structures.
Let’s understand why this is possible when tuples are read only data types. In Python tuples store memory references of its elements and until those memory references are not modified by any assignment it will not throw any error.
So, in the above case if you observe we are not modifying the elements with a new value i.e. last element in tuple which is a list or tuple or set as observed in different use cases are not getting changed to a different value altogether. Instead the values inside the element (list, dictionary or set) are getting changed which is fine with Tuples data type.