What are Tuples in Python.
By definition Tuples are immutable data types which store a collection of objects.
Creating a tuple is as simple as putting different comma-separated values. Optionally you can put these comma-separated values between parentheses also.
Like string indices, tuple indices start at 0, and they can be sliced, concatenated, and so on.
Tuples are immutable which means you cannot update or change the values of tuple elements. You are able to take portions of existing tuples to create new tuples as the following example demonstrates −
Tuples are immutable but potential changeable.
Python tuples have a surprising trait: they are immutable, but their values may change. This may happen when a tuple holds a reference to any mutable object, such as a list. To understand this a good first step is to challenge the common notion that variables are like boxes where you store data.
Python variables are like reference variables in Java, so it’s better to think of them as labels attached to objects.
Python variables are Name alias
Here is a Tweedledum’s and Tweedledee’s twins example inspired by Lewis Carroll’s:
It’s clear that dum and dee refer to objects that are equal, but not to the same object. They have distinct identities.
Tweedledum decided to become a rapper, adopting the stage name T-Doom. This is how we can express this in Python:
So, t_doom and dum are equal — but Alice might complain that it’s foolish to say that, because t_doom and dum refer to the same person: t_doom is dum.
The names t_doom and dum are aliases. The official Python docs often refer to variables as “names”. Variables are names we give to objects. Alternate names are aliases. That helps freeing our mind from the idea that variables are like boxes.
After much practice, T-Doom is now a skilled rapper
T-Doom acquired the ‘rap’ skill, and so did Tweedledum — of course, they are one and the same. If t_doom was a box containing a str and a list, how could you explain that appending to the list in t_doom also changes the list in the dum box? However, it makes perfect sense if you see variables as labels.
The label analogy is much better because aliasing is explained simply as an object with two or more labels. In the example, t_doom and skills are two names given to the same list object, just as dum and t_doom are two names given to the same tuple object.
What is immutable is the physical content of a tuple, consisting of the object references only. The value of the list referenced by dum changed, but the referenced object id is still the same. A tuple has no way of preventing changes to the values of its items, which are independent objects and may be reached through references outside of the tuple, like the skills name we used earlier. Lists and other mutable objects inside tuples may change, but their ids will always be the same.
After dum became a rapper, the twin brothers are no longer equal:
We have two tuples that were created equal, but now they are different.
The other built-in immutable collection type in Python, frozenset, does not suffer from the problem of being immutable yet potentially changing in value. That’s because a frozenset (or a plain set, for that matter) may only hold references to hashable objects, and the value of hashable objects may never change, by definition.
Tuples are commonly used as dict keys, and those must be hashable — just as set elements. So, are tuples hashable or not? The right answer is: some tuples are hashable. The value of a tuple holding a mutable object may change, and such a tuple is not hashable. To be used as a dict key or set element, the tuple must be made only of hashable objects.
Our tuples named dum and dee are unhashable because each contains a list reference, and lists are unhashable.
hence for tuples, make sure they only hold references to immutable objects before trying to use them as dictionary keys or put them in sets.
Thanks for reading. If you still have questions, feel free to reach out either in the comments section or on email.