amandi
amandi

Reputation: 283

Are tuples in Python immutable?

It says

A tuple can not be changed in any way once it is created.

But when I do the following:

t1=(4,5,8,2,3)
t1=t1+(7,1)
print(t1)

the tuple is changing to (4, 5, 8, 2, 3, 7, 1); why is that? What is really meant by "tuples are immutable"?

Upvotes: 4

Views: 2037

Answers (5)

Kiran Kumar Kotari
Kiran Kumar Kotari

Reputation: 1160

I can't say Yes. Python tuples have a surprising trait: they are immutable, but their values may change. This may happen when a tuple holds a ref. to any mutable object, such as a dict, list...

>>> t1 = ('Bangalore', ['HSR', 'Koramangala'])
>>> print(t1)
('Bangalore', ['HSR', 'Koramangala'])
>>> print(id(t1)) # ID of tuple
4619850952

>>> place = t1[1]
>>> place.append('Silk Board')  # Adding new value to the list
>>> print(t1) 
('Bangalore', ['HSR', 'Koramangala', 'Silk Board'])
# Surprisingly tuple changed, let's check the ID
>>> print(id(t1)) # No change in the ID of tuple
4619850952

>>> print(t1[0])
Bangalore
>>> print(id(t1[0])) # ID of tuple's first element
4641176176
>>> print(id(t1[1])) # ID of tuple's second element (List)
4639158024
# These are the ref. id's of the tuple

>>> place.append('Agara')
>>> print(t1) 
('Bangalore', ['HSR', 'Koramangala', 'Silk Board', 'Agara'])
>>> print(id(t1))
4619850952
# Still no change, are they Immutable ??

>>> print(id(t1[1])) # The tuple having a ref. of Mutable object
4639158024

In the above example the id's of tuple and list didn't change. This is happening due to reference which is mapped to the tuple not the value.

Upvotes: 1

A.J. Uppal
A.J. Uppal

Reputation: 19264

Basically when you call t1=t1+(7,1), you are reassigning t1 to a different memory location. What python means by immutable, is that you can't change them by slicing:

>>> t1=(4,5,8,2,3)
>>> t1[0] = 9
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> 

Because this creates a new tuple:

>>> t1=(4,5,8,2,3)
>>> id(t1)
4467745808
>>> t1 = t1+(9,)
>>> id(t1)
4468302112
>>> 

As you can see with lists, they keep the id:

>>> lst = [4, 5, 8, 2, 3]
>>> id(lst)
4468230480
>>> lst[0] = 6
>>> id(lst)
4468230480
>>> 

That is python's definition of immutability.

Upvotes: 2

jonrsharpe
jonrsharpe

Reputation: 121986

Yes, tuples are immutable; once created, they cannot be changed. t1=t1+(7,1) creates a new tuple and assigns it to the name t1. It does not change the tuple object originally referenced by that name.

Demo:

>>> t = (1, 2, 3)
>>> id(t)
4365928632
>>> t = t + (4, 5)
>>> id(t)
4354884624 # different id, different object

Upvotes: 10

user395760
user395760

Reputation:

No tuple is changing in your code. The name t1 is made to refer to a new, distinct tuple. The original tuple object never changed, you just stopped using it.

Upvotes: 3

Joran Beasley
Joran Beasley

Reputation: 113950

yes they are immutable

t1 = t1 + (7,1)

Is creating a new tuple ... not modifying the old one

try

t1[0] = 5

Upvotes: 2

Related Questions