For python beginners, the differences between lists and tuples might not be as clear as to those who already got some experience using them.

python

Let’s review the key differences between the two. hopefully it will help you choose the right type in your code and understand that although this post called ‘lists vs. tuples’ there is no real match here. each type has his own use case.

Mutability

Mutability talks about the capability of object to change or alter. This is one of noticeable difference between the two types.

First, let’s use list:

Now let’s try the same on tuple:

As you can see, List is mutable in python, which means you can change it. You can modify the current objects in list, add new and remove them.

For tuple all the above mentioned actions, will not work as tuple is immutable. You can’t add or remove elements from a tuple.

Heterogeneous & Homogeneous

Tuples usually contain an heterogeneous sequence of elements, meaning the elements are of different types. Lists on the other hand, contain an homogeneous sequences which means only one data type for the whole list.

This is in my opinion an interesting distinction between the two, since it’s not explicit as other and it’s more of a semantic difference.  Eventually, you can use single type in tuple and different types in a list.

There are several analogies used to emphasize this difference. My favorite one is the ‘phone app’.  To save phone contacts, you will probably want to use list, because your contacts properties can be changed – phone number, image, even name sometimes.

To save ‘call logs’ you will want to use tuples. Call logs are used for viewing only. You can’t change call logs content.  You don’t want anyone to change the history details. Only to watch them. (The only problem with this example might be that you can not add objects to tuples and for ‘call logs’ you would want to add additional calls. but let’s assume each time new call made, a new tuple being created 🙂  )

Size

The size of tuple object in memory is smaller than list. but that’s only noticeable when using big objects.

As you can see, the tuple is much smaller in size than list. That makes tuple operations faster.

Dictionary identifier

You can use tuple as a dictionary identifier, but not list.