Confusion about the immutability of tuples

Viewed 11

I am learning about tuples. My book is trying to make the following point.Suppose we have a tuple with three lists as elements. So, for example, we have:

life = (canada,usa,mexico)

canada = ['Canada', 76.5]
usa = ['United States', 75.5]
mexico = ['Mexico', 72.0]

Now, lists are mutable, but tuples are not. So, I can update the mexico list:

mexico = ['Mexico', 73]

But if I then call the tuple, the change doesn't show:

>>> life

(['Canada', 76.5],['United States', 75.5],['Mexico', 72.0])

OK, but then I updated mexico this way:

mexico[1] = 74

and this time the tuple DID update:

>>> life

(['Canada', 76.5],['United States', 75.5],['Mexico', 74])

What's going on here?

0 Answers
Related