Python Tuples: When to Use Tuples vs. Lists

lists and dictionaries are the most commonly used built-in data types in Python, which also makes them the most well-known data types in Python. When it comes to tupleCapturing the details, how they differ from the lists is not an easy task for beginners as they are very similar to each other.

In this article I will explain main differences by providing examples of different use cases to better explain usage tuple Above lists.

Python Tuples: When to Use Tuples vs. Lists

The main difference between tuples and lists is that tuples are immutable objects but lists are mutable. This means that tuples cannot be modified, 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 the lookup value. If you have data that should not change, you should choose the 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 collections of data
  • Tuples and lists are both heterogeneous data types, which means you can store any kind of data type.
  • Tuples and lists are both ordered, meaning the order in which you place the elements is preserved.
  • Tuples and lists are both sequential data types, so you can iterate over the elements they contain.
  • Elements of tuples and lists can be accessed via an integer subscript operator, specified in square brackets. [index].

So… how are they different?

The main difference between tuples and lists is that during the Tuples are immutable objects, Lists are changeable. This means that tuples cannot be modified, while lists can be modified.

Let’s further understand how this can affect our code in terms of time and memory efficiency.

Because lists are mutable, Python must allocate an additional block of memory in case the list object needs to be increased in size after creation. In contrast, since tuples are immutable and of fixed size, Python allocates only the minimum block of memory required for the data.

The result are tuples more memory efficient as lists.

Let’s verify this in the following code block:

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 code snippet above, the memory required for identical list and tuple objects is different.

Again, when it comes to time efficiency, tuples have a slight advantage over the lists, especially when we consider the 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

Python Tutorial for Beginners: Lists, Tuples and Sets | Corey Shepherd

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, that of course depends on your needs.

There may be times when you do not want your data to be changed. If you have data that should not be changed in the first place, you should choose the tuple data type via lists.

However, if you know that the data will grow and shrink during the runtime of the application, you need to decide on the list data type.

More from our software engineering expertsBuild the classic snake game using Processing Library and Java

Python Tuple: The Snack

In this short article I explained the differences between tuples and lists and when we should use tuples in Python. The most important findings are:

  • The main difference between the tuples and lists is that during the Tuples are immutable objects the Lists are changeable. This means that tuples cannot be modified, while the lists can be modified.
  • are tuples more memory efficient than the lists.
  • When it comes to time efficiency, tuples have a slight advantage over the lists, especially when we consider the lookup value.
  • If you have data that should not change, you should choose the tuple data type over lists.

I hope you found the article useful and You start using tuple data types in your own code.

Leave a Comment

%d bloggers like this: