TUPLES in Python

What happens to immutable objects, when they contain mutable types in Python : Python Tuples

This blog assumes you know about Python “lists“.

Today’s tidbit is about Tuples in Python. Why? : Because tuples are special. They are immutable but their values can change !  Wondering what does this mean? Then, read on.


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 don’t 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 adding an example code snippet, 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. 

tuple_test.png

Snapshot 1. Immutable property of Tuples


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 latter’s 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. Here goes an example :

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 object’s 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.

t_w_l.png

Snapshot 2. Tuple with a List object

An object’s 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. 😉

I will add my take soon. Do check back. Until then , keep decoding! 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: