Python Tuples: When to Use Them Over Lists | by Erdem Isbilen | Towards Data Science

Lists and dictionaries are the most widely used built-in data types in Python. This makes them also the best-known data types in Python. When it comes to tuples, grasping the details of how they differ from the lists is not an easy task for the beginners as they are very similar to each other.
In this article, I will explain key differences by providing examples for various use cases to better explain when to use tuples over lists.
They are both used to store collection of data
They are both heterogeneous data types means that you can store any kind of data type
They are both ordered means the order in which you put the items are kept.
They are both sequential data types so you can iterate over the items contained.
Items of both types can be accessed by an integer index operator, provided in square brackets, [index]
So well, how they differ then?
The key difference between the tuples and lists is that while the tuples are immutable objects the lists are mutable. This means that tuples cannot be changed while the lists can be modified.
Let’s further understand how this effect our code in terms of time and memory efficiency.
As lists are mutable, Python needs to allocate an extra memory block in case there is a need to extend the size of the list object after it is created. In contrary, as tuples are immutable and fixed size, Python allocates just the minimum memory block required for the data.
As a result, tuples are more memory efficient than the lists.
Let’s check this in the code block below;
import sysa_list = list()a_tuple = tuple()a_list = [1,2,3,4,5]a_tuple = (1,2,3,4,5)print(sys.getsizeof(a_list))print(sys.getsizeof(a_tuple))Output:104 (bytes for the list object88 (bytes for the tuple object)
As you can see from the output of the above code snippet, the memory required for the identical list and tuple objects is different.
When it comes to the time efficiency, again tuples have a slight advantage over the lists especially when lookup to a value is considered.
import sys, platformimport timeprint(platform.python_version())start_time = time.time()b_list = list(range(10000000))end_time = time.time()print("Instantiation time for LIST:", end_time - start_time)start_time = time.time()b_tuple = tuple(range(10000000))end_time = time.time()print("Instantiation time for TUPLE:", end_time - start_time)start_time = time.time()for item in b_list:  aa = b_list[20000]end_time = time.time()print("Lookup time for LIST: ", end_time - start_time)start_time = time.time()for item in b_tuple:  aa = b_tuple[20000]end_time = time.time()print("Lookup time for TUPLE: ", end_time - start_time)Output:3.6.9Instantiation time for LIST: 0.4149961471557617 Instantiation time for TUPLE: 0.4139530658721924 Lookup time for LIST:  0.8162095546722412 Lookup time for TUPLE:  0.7768714427947998
Well, obviously this depends on your needs.
There may be some occasions you specifically do not what data to be changed. If you have data which is not meant to be changed in the first place, you should choose tuple data type over lists.
But if you know that the data will grow and shrink during the runtime of the application, you need to go with the list data type.
In this short article, I have explained what are the differences between the tuples and lists and when we should use tuples in Python. The key takeaways are;
The key difference between the tuples and lists is that while the tuples are immutable objects the lists are mutable. This means that tuples cannot be changed while the lists can be modified.
Tuples are more memory efficient than the lists.
When it comes to the time efficiency, again tuples have a slight advantage over the lists especially when lookup to a value is considered.
If you have data which is not meant to be changed in the first place, you should choose tuple data type over lists.
I hope you have found the article useful and you will start using tuple data types in your own code.
See my latest article to learn more about Python Type Hints