Lists and dictionaries are the most widely used built-in data types in Python, which also makes them 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 because they are very similar to each other.
Python Tuples: When to Use Tuples vs. Lists
In this article, I will explain key differences by providing examples for various use cases to better explain when to use tuples over lists.
Python Tuples vs. Lists
Tuples and lists in Python both have similarities and differences, making them useful for certain scenarios when coding.
Similarities Between Python Tuples and Lists
- Tuples and lists are both used to store collections of data.
- Tuples and lists are both heterogeneous data types, which means that you can store any kind of data type.
- Tuples and lists are both ordered, which means the order that you put the items in are kept.
- Tuples and lists are both sequential data types so you can iterate over the items contained.
- Items of both tuples and lists can be accessed by an integer index operator, provided in square brackets, [index].
Differences Between Python Tuples and Lists
- Tuples are immutable objects and lists are mutable objects.
- Once defined, tuples have a fixed length and lists have a dynamic length.
- Tuples use less memory and are faster to access than to lists.
- Tuple syntax uses round brackets or parenthesis, and list syntax uses square brackets.
Python Tuple vs. List Code
Let’s further understand how the differences in tuples and lists may affect our Python 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 we create it. In contrast, as tuples are immutable and of a fixed size, Python allocates only the minimum memory block required for the data.
As a result, tuples are more memory-efficient than lists.
Let’s check this in the code block below:
import sys
a_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 object
88 (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 we consider lookup value.
import sys, platform
import time
print(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.9
Instantiation time for LIST: 0.4149961471557617
Instantiation time for TUPLE: 0.4139530658721924
Lookup time for LIST: 0.8162095546722412
Lookup time for TUPLE: 0.7768714427947998
When Do I Use Tuples vs. Lists?
When to use tuples or lists depends on your needs.
There may be some occasions when you don’t want your data to be changed. If you have data that’s not meant to be changed in the first place — such as critical information or records — 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.
Frequently Asked Questions
What are the tuples in Python?
A tuple is a built-in data structure in Python used to store multiple items into a single variable. Tuples are similar to lists in Python in that they can both compile an ordered collection of items, except tuples are immutable and lists are mutable.
What is an advantage of using a tuple rather than a list?
An advantage of Python tuples is that they are immutable, meaning that the items and their order in the tuple cannot be changed once it is defined. This can help make data stored in a tuple more secure, and makes tuples faster and more memory-efficient than lists.
When would you use a list vs. tuple vs. a set in Python?
In Python, lists are best used to order items under one variable and have items able to be added, removed, replaced or changed. Tuples are best used to order items under one variable and have its items be unchangeable. Sets are best used when wanting to collect items in any order under one variable, and have items only be able to be added or removed.