List and tuple are two of the four built-in data types for storing data in Python. The list type is a dynamic, mutable data structure, whereas a tuple is a static, immutable data structure.
This tutorial will cover the differences between a List and Tuple in Python.
Table of contents
List and Tuple in Python
Both list and tuple are containers that allow you to organise data in an ordered collection of one or more items. You can check whether an object is a list or tuple using the type() built-in function. Let’s look at an example where we use the type() function for a list and a tuple:
my_tuple = (2, 3, 4, 5)
my_list = [2, 3, 4, 5]
empty_tuple = tuple()
another_tuple = tuple(my_list)
print(type(my_tuple))
print(type(empty_tuple))
print(type(another_tuple))
print(type(my_list))
We define a tuple using parenthesis and define a list using square brackets. You can also use the tuple() constructor method to create an empty tuple or convert a list to a tuple. Let’s run the code to get the output:
≺class 'tuple'≻
≺class 'tuple'≻
≺class 'tuple'≻
≺class 'list'≻
Differences Between List and Tuple in Python
Lists are mutable objects in Python, which means you can change the items within a list after you have defined it, and you can continually change a list. Tuples are immutable objects in Python, which means you cannot change the items within a tuple once you have defined the tuple. Let’s look at an example where we try to change a pre-defined tuple:
names = ("Connor", "Logan", "Roman", "Siobhan")
names.append("Kendall")
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
1 names.append("Kendall")
AttributeError: 'tuple' object has no attribute 'append'
names.pop()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
1 names.pop()
AttributeError: 'tuple' object has no attribute 'pop'
names[2] = "Kendall"
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
1 names[2] = "Kendall"
TypeError: 'tuple' object does not support item assignment
The above code shows that tuple does not support adding, removing, replacing, or reassigning items within a tuple once we create it.
Let’s try to perform those same operations on a list
names = ["Connor", "Logan", "Roman", "Siobhan"]
names.append("Kendall")
print(names)
['Connor', 'Logan', 'Roman', 'Siobhan', 'Kendall']
names = ["Connor", "Logan", "Roman", "Siobhan"]
names.pop()
print(names)
['Connor', 'Logan', 'Roman']
names = ["Connor", "Logan", "Roman", "Siobhan"]
names[2] = "Kendall"
print(names)
['Connor', 'Logan', 'Kendall', 'Siobhan']
The above code shows that we can manipulate the contents of a list once after we create it. A list can grow and shrink during a program’s lifecycle. The mutable nature of a list is helpful for cases where we want to edit data.
Similarites Between List and Tuple in Python
List and tuple Can be Empty, Store One Item or Multiple Items
You can use both a tuple or a list to store multiple items under a single variable. Both objects can be empty and can contain only one item. To create a tuple with one item, you must use a trailing comma as shown below:
a_tuple = (7,)
print(type(a_tuple))
≺class 'tuple'≻
If you do not use a trailing comma, the Python interpreter will not interpret the object as a tuple:
a_tuple = (7)
print(type(a_tuple))
≺class 'int'≻
When creating a list with one item, you do not need to use a trailing comma:
a_list = [7]
print(type(a_list))
≺class 'list'≻
List and Tuple Can Store Same Data Types or Mixed Data Types
With both list and tuple objects, you can create a data collection of the same type. You can also use tuple or list to store mixed data types. Let’s look at an example of a list and tuple storing mixed data:
some_data_list = [7, True, "Spain", 4.8, False, "Geneva"]
some_data_tuple = (3, False, "Greece", True, 100, "Nice")
print(some_data_list)
print(some_data_tuple)
[7, True, 'Spain', 4.8, False, 'Geneva']
(3, False, 'Greece', True, 100, 'Nice')
List and Tuple Support Unpacking
Unpacking is the process of splitting packed values into individual elements. All of the built-in container data types support unpacking. Let’s look at an example of unpacking a tuple:
particles = ("electron", "muon", "proton", "neutron")
e, m, p, n = particles
print(e)
print(m)
print(p)
print(n)
In the above code, we unpack the variables packed inside the tuple into individual variables. Let’s run the code to get the output:
electron
muon
proton
neutron
Similarly, we can unpack a list as follows:
particles = ["electron", "muon", "proton", "neutron"]
e, m, p, n = particles
print(e)
print(m)
print(p)
print(n)
electron
muon
proton
neutron
For further reading on unpacking, go to the article: How to Solve Python TypeError: cannot unpack non-iterable NoneType object.
List and Tuple Can Store Duplicates
You can store duplicate items in both lists and tuples, and you cannot do this for sets and dictionaries. Let’s look at an example of a list and a tuple holding duplicate items:
a_list = [2, 2, 4, 4, 5, 6, 6]
a_tuple =(2, 2, 4, 4, 5, 6, 6)
print(a_list)
print(a_tuple)
[2, 2, 4, 4, 5, 6, 6]
(2, 2, 4, 4, 5, 6, 6)
If we pass either a list or a tuple to a set, the set object will store unique values:
a_set_1 = set(a_list)
a_set_2 = set(a_tuple)
print(a_set_1)
print(a_set_2)
{2, 4, 5, 6}
{2, 4, 5, 6}
List and Tuple Support Indexing to Access Items
List and tuple both preserve the order of the collection of items they store. Each value in a tuple and a list has a unique identifier called an index. If we want to access a specific value from a list or a tuple, we need to reference the index of that value. Indexing in Python starts at 0 and increments by 1 for each following item, and the index value must be within square brackets. Let’s look at an example of indexing both a list and a tuple:
names_tuple = ("Connor", "Logan", "Roman", "Siobhan")
names_list = ["Connor", "Logan", "Roman", "Siobhan"]
print(names_tuple[1])
print(names_list[3])
Logan
Siobhan
For further reading on indexing a list, go to the article: How to Solve Python TypeError: list indices must be integers, not tuple.
Summary
Congratulations on reading to the end of this tutorial! To summarise, the differences between tuples and lists are:
- Tuples are immutable objects, which means the data inside a tuple will not change during your program’s lifecycle. Using tuples are preferable if you want to guarantee your data will always stay the same.
- Lists are mutable objects, which means you can change the data inside a list during the your program’s lifecycle. Using lists are preferable if you need to change your data.
The similarities between tuples and lists are:
- Both can be empty, store one item or store multiple items.
- Both can store collections of the same data type or mixed data type.
- Both can store duplicate values.
- Both support unpacking.
- Both support indexing to access their items.
For further reading on manipulation of lists and tuples, go to the article: How to Sort a List of Tuples in Python.
Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.