The Mystique of Tuple Immutability in Python
If Tuples are really immutable, then how is this possible???
When we first encounter tuples in Python, we’re often introduced to them with a key characteristic: immutability. This means once a tuple is created, its content remains unchanged for its entire existence. This seems straightforward — tuples are like unchangeable snapshots of data.

Let’s start with a simple example to illustrate this concept:
# Creating a tuple
my_tuple = (1, 2, 3)
# Trying to modify an element of the tuple
try:
my_tuple[0] = 10
except TypeError as e:
print("Error:", e)
Output:
Error: 'tuple' object does not support item assignment
Breaking Down the Example
Creating the Tuple:
my_tuple = (1, 2, 3)
initializes a tuple with three integer elements.Attempting to Modify: When we try to modify an element (
my_tuple[0] = 10
), Python raises aTypeError
. This error confirms that tuples do not support item assignment, reinforcing their immutability.
So far, so good. This fits neatly with our understanding of tuples. But let’s dive a little deeper and explore a twist on this concept.
A Tuple with Mutable Contents
Now, let’s create a tuple containing mutable objects, such as lists. We’ll explore two scenarios: one where we attempt to modify the tuple itself and another where we alter the contents of the mutable objects inside the tuple.
# Creating a tuple containing a list
my_tuple = ([1, 2, 3], [4, 5, 6])
# Printing the original tuple
print("Original tuple:", my_tuple)
# Attempting to change the tuple itself (will raise an error)
try:
my_tuple.append([7, 8])
except AttributeError as e:
print("Error:", e)
# Modifying an element within the list inside the tuple
my_tuple[0].append(4)
# Printing the modified tuple
print("Modified tuple:", my_tuple)
Output:
Original tuple: ([1, 2, 3], [4, 5, 6])
Error: 'tuple' object has no attribute 'append'
Modified tuple: ([1, 2, 3, 4], [4, 5, 6])
Breaking Down the Example
Creating the Tuple:
my_tuple = ([1, 2, 3], [4, 5, 6])
initializes a tuple with two lists.Attempting to Modify the Tuple: Trying
my_tuple.append([7, 8])
results in anAttributeError
because tuples lack anappend
method, highlighting that the tuple structure itself is immutable.Modifying Nested Lists: However,
my_tuple[0].append(4)
successfully appends an item to the list inside the tuple.
This demonstrates that while the tuple’s structure can’t be changed, the mutable objects within it can still be modified.
The Final Puzzle
Let’s tackle a more intriguing scenario:
our_tuple = (1, 2, [3, 4])
our_tuple[2] += [5, 6]
What do you expect the output to be? If you guessed that an error would occur, you’re correct!
However, when we try to print our_tuple
, you might be surprised by the result. Here’s what happens:
print(our_tuple)
Output:
(1, 2, [3, 4, 5, 6])
Explanation:
Tuple Creation:
our_tuple = (1, 2, [3, 4])
creates a tuple with two integers and a list.Modifying the List:
our_tuple[2] += [5, 6]
appends[5, 6]
to the list at index 2. Althoughour_tuple
itself cannot be changed, the list it contains is mutable and can be modified. Therefore, even though an error mesaage is printed, the tuple gets updated also. This is something that one needs to be aware of.
This showcases an essential nuance of Python tuples: while the tuple structure itself is immutable, the mutable objects contained within can still be altered. This blend of immutability and mutability can be both powerful and, at times, surprising.