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.
In this article, I will explain key differences by providing examples for various use cases to better explain when to use tuples over lists.
The key difference between tuples and lists is that while tuples are immutable objects, lists are mutable. This means tuples cannot be changed while lists can be modified. Tuples are also more memory efficient than the lists. When it comes to time efficiency, tuples have a slight advantage over lists especially when we consider lookup value. If you have data that shouldn’t change, you should choose tuple data type over lists.
More on Python ListsHow to Append Lists in Python
Python Tuples vs. Lists
- Tuples and lists are both used to store collection of data
- Tuples and lists are both heterogeneous data types means that you can store any kind of data type.
- Tuples and lists are both ordered means the order in which you put the items 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].
So…how do they differ?
The key difference between tuples and lists is that while the tuples are immutable objects, lists are mutable. This means that tuples cannot be changed while lists can be modified.
Let’s further understand how this may affect 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 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
More Tutorials From Our Python ExpertsWhat Is the @ Symbol in Python and How Do I Use It?
When Do I Use Tuples vs. Lists?
Well, obviously this 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, 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.
More From Our Software Engineering ExpertsCreate the Classic Snake Game With Processing Library and Java
Python Tuples: The Takeaway
In this short article, I have explained the differences between 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, tuples have a slight advantage over the lists especially when we consider lookup value.
- If you have data that shouldn’t change, 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.
Author: Bryan Hall
Last Updated: 1704671403
Views: 1902
Rating: 3.7 / 5 (118 voted)
Reviews: 80% of readers found this page helpful
Name: Bryan Hall
Birthday: 1934-12-18
Address: 833 Hamilton Turnpike, Lake Kellyton, IN 65024
Phone: +3600364566603382
Job: Investment Banker
Hobby: Stamp Collecting, Skateboarding, Playing Piano, Golf, Coin Collecting, Table Tennis, Astronomy
Introduction: My name is Bryan Hall, I am a frank, Colorful, important, unswerving, bold, striking, Open person who loves writing and wants to share my knowledge and understanding with you.