Is a tuple in a list immutable?

Tuples in Python art of mutability and immutability

geekgirldecodes

Mar 21, 2018·3 min read

This blog assumes you know about Python lists.

Heres a tidbit about Tuples in Python. Why? Because Tuples are special. They are immutable. But, does being immutable only mean that they cant change the values held? What does immutability actually mean? Lets find out.

What are tuples?

Tuples refer to a sequence of immutable Python objects. They are similar to lists except the objects are not mutable. Tuples use parentheses, lists use square braces. Tuples can store heterogeneous data together. For example : if you want to share a ip address of a host or domain name along with port for access

machine1=[10.1.1.2, 80];

More details here , under server_address parameter.

You use tuples when you dont want to edit the values once defined, as in this case. If you want to hold list of heterogeneous items that need to be modified, you would use a list.

Immutable Property

To get a good understanding, I would suggest that you check this : Data model in Python 3.

I am also adding an example code snippet below [Fig 1.], so it gives you a clear picture what it means to be immutable in Python.

Since tuples are immutable objects in Python, when you have a tuple1+=[4,] kind of operation it essentially means a new object shall be created. This is demonstrated by the difference in the ids of the first tuple1 instance and tuple1 post the addition of new elements : as marked in RED below. Whereas, with a list, even after addition of new elements, the id remains the same- because they are mutable data structure in Python.

Fig 1. Immutable objects in Python

Immutable Tuples and mutable objects

Quoting from the data model page :

The value of an immutable container object that contains a reference to a mutable object can change when the latters value is changed; however the container is still considered immutable, because the collection of objects it contains cannot be changed. So, immutability is not strictly the same as having an unchangeable value, it is more subtle.

Essentially, when tuples hold a list object, Tuples are the immutable containers to mutable object whose value can change. This means values contained in the Tuple cannot change, while the data contained in the list object can still change.

An example would most definitely add clarity.

Heres an example [Refer Fig 2 below]:

Marked with a red line is the list that is initialized and the one that is held as one of the objects in the Tuple [container] tuple_with_list.

Then, marked in yellow are the modifications to the list, addition of two new values 4 and 5.

So, essentially , a mutable objects value can be updated even when contained within an immutable container [Tuple].

This, as expected updates the value of the tuple. And, as highlighted with a blue line, the id of the Tuple will remain the same before and after updates to the list object.

Fig 2. Using Tuples

An objects mutability is determined by its type; for instance, numbers, strings and tuples are immutable, while dictionaries and lists are mutable.

Are tuples better than Lists?

What do you think ? Do let me know in the comments below. Until then , keep decoding and of course clapping! :]

For more such tidbits visit : www.geekgirldecodes.com

Note :

If you liked what you read, dont forget to clap and let me know it was useful. Your claps also help this content reach other folks on this platform. Thank you! :]

Video liên quan

Chủ Đề