python tuple is immutable – so why can I add elements to it

Question:

I’ve been using Python for some time already and today while reading the following code snippet:

>>> a = (1,2)
>>> a += (3,4)
>>> a
(1, 2, 3, 4)

I asked myself a question: how come python tuples are immutable and I can use an += operator on them (or, more generally, why can I modify a tuple)? And I couldn’t answer myself.

I get the idea of immutability, and, although they’re not as popular as lists, tuples are useful in python. But being immutable and being able to modify length seems contradictory to me…

Asked By: ducin

||

Answers:

5 is immutable, too. When you have an immutable data structure, a += b is equivalent to a = a + b, so a new number, tuple or whatever is created.

When doing this with mutable structures, the structure is changed.

Example:

>>> tup = (1, 2, 3)
>>> id(tup)
140153476307856
>>> tup += (4, 5)
>>> id(tup)
140153479825840

See how the id changed? That means it’s a different object.

Now with a list, which is mutable:

>>> lst = [1, 2, 3]
>>> id(lst)
140153476247704
>>> lst += [4, 5]
>>> id(lst)
140153476247704

The id says the same.

Answered By: Veedrac

you are not modifying it, you created a new tuple and changed the content of the a variable

try a[0] = a[0] + 1 to see the immutability

Answered By: prgao

Whether += modifies the object in-place or not is up to the object. With a tuple, you aren’t modifying the object, as you can see if you create another variable pointing to the same object:

>>> x = (1, 2)
>>> y = x
>>> x += (3, 4)
>>> y
(1, 2)

With mutable objects such as lists, you will see that the value changes, showing up under all its names:

>>> x = [1, 2]
>>> y = x
>>> x += [3, 4]
>>> y
[1, 2, 3, 4]
Answered By: BrenBarn

Adding some context on top of veedrac‘s answer.

When using augmented assignment operators with sequences (+= or *=), the special/dunder methods __iadd__ (in-place addition) or __imult__ (in-place multiplication) will be called respectively for += or *=. Those methods are implemented for list but not for tuple. If those methods are not implemented Python will fall back on the __add__ or __mult__ which both return a new object.

Those are the dunder methods being called when directly calling the + or * operator on list or tuple. (l3 = l1 + l2 where l1 and l2 are lists or t2 = t1 * 2 for t2 being a tuple)

This explains the difference of behavior between:

  1. augmented assignment operators on tuple
>>> tup = (1, 2, 3)
>>> id(tup)
140153476307856
>>> tup += (4, 5)
>>> id(tup)
140153479825840
  1. augmented assignment operators on list
>>> lst = [1, 2, 3]
>>> id(lst)
140153476247704
>>> lst += [4, 5]
>>> id(lst)
140153476247704

Please note that using those operations on tuple in a loop is inefficient because the interpreter has to copy whole target object first before doing the concatenation and returning a new object, which isn’t the case when the operation is done in-place.

import time

start_time = time.time()
l1 = [1, 2, 3]
l2 = [4, 5]

for _ in range(100000):
    l1 += l2
print("--- list:  %s seconds ---" % (time.time() - start_time))

start_time = time.time()
t1 = (1, 2, 3)
t2 = (4, 5)

for _ in range(100000):
    t1 += t2
print("--- tuple:  %s seconds ---" % (time.time() - start_time))

gives as output:

--- list:  0.0055124759674072266 seconds ---
--- tuple:  20.920572996139526 seconds ---
Answered By: Allan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.