Lists and tuples are two of the most commonly used Python objects that store data in the form of a collection of items. Both lists and tuples can contain items of the same or different data types. In this article, you will see how lists and tuples in Python differ from each other. So let’s begin.
Table of Contents
Difference in Syntax
The most visible difference between a list and a tuple lies in the way both of them are declared. To create a Python list, a collection of items are passed inside square brackets “[]”. On the other hand, to create a tuple, items are passed inside parentheses brackets “()”. Here is an example:
cars_list = ["Honda", "Toyota", "Ford", "BWM"] cars_tuple = ("Honda", "Toyota", "Ford", "BWM") print(type(cars_list)) print(cars_list) print(type(cars_tuple)) print(cars_tuple)
The script above creates a list “cars_list” and a tuple “cars_tuple”. You can see that list is created using square brackets “[]”, while the tuple is created using parentheses “()”. Both the list and tuple contains 4 items. Next, the print() method is being used to print the type and items stored in the list and the tuple. Here is the output of the script above.
<class 'list'> ['Honda', 'Toyota', 'Ford', 'BWM'] <class 'tuple'> ('Honda', 'Toyota', 'Ford', 'BWM')
Mutable vs Immutable
A list is mutable which means that you can update items in a list or add or remove items in a list. On the other hand, once a tuple is created, you cannot update it. Let’s see this with the help of an example.
cars_list = ["Honda", "Toyota", "Ford", "BWM"] cars_list[1] = "Mercedez" print(cars_list)
In the script above, you define a list of 4 items. Next, the item at the second index (list indexes start from 0) is updated. Finally, the print() method prints the updated list on the console. Here is the output:
['Honda', 'Mercedez', 'Ford', 'BWM']
You can see that the second index now contains the updated item i.e. “Mercedez”.
Let’s now try to update a tuple in the same way:
cars_tuple= ("Honda", "Toyota", "Ford", "BWM") cars_tuple[1] = "Mercedez" print(cars_tuple)
Once you execute the above script, you will see the following output:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-9-6b25ef8413d2> in <module> 1 cars_tuple= ("Honda", "Toyota", "Ford", "BWM") ----> 2 cars_tuple[1] = "Mercedez" 3 print(cars_tuple) TypeError: 'tuple' object does not support item assignment
The output basically says that you cannot update a tuple.
Let’s see another example. This time you will try to remove an item from a list and then from a tuple and see how they compare. Let’s first remove an item from a list. Run the following script:
cars_list = ["Honda", "Toyota", "Ford", "BWM"] cars_list.remove("Ford") print(cars_list)
The above script will remove the item “Ford” from the “cars_list” and will display the remaining items on the console. Here is the result:
['Honda', 'Toyota', 'BWM']
Now let’s try to remove an item from a tuple. Run the following script:
cars_tuple = ("Honda", "Toyota", "Ford", "BWM") cars_tuple.remove("Ford") print(cars_tuple)
You should see the following result when you run the above script:
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-10-4241135bac76> in <module> 1 cars_tuple = ("Honda", "Toyota", "Ford", "BWM") ----> 2 cars_tuple.remove("Ford") 3 print(cars_tuple) AttributeError: 'tuple' object has no attribute 'remove'
The above output basically says that you cannot use the remove() method to remove an item from a tuple. In other words, you cannot modify a tuple.
The difference in Memory Size
The other major difference between a list and a tuple lies in the size they occupy in a system’s memory. Typically, lists occupy more size than a tuple with the same set of elements. Let’s verify this.
cars_list = [“Honda”, “Toyota”, “Ford”, “BWM”]
cars_tuple = (“Honda”, “Toyota”, “Ford”, “BWM”)
print(cars_list.__sizeof__())
print(cars_tuple.__sizeof__())
In the above script, we create a list and a tuple with four similar string items. Next, the sizes of both list and tuple are displayed via the __sizeof__() method. Here is the output of the script:
72 56
The output shows that a list of four items occupies 72 bytes in memory. On the other hand, a tuple with the same four items occupies 56 bytes.
Hashable vs. Unhashable
A tuple in Python is hashable. On the other hand, a list is not hashable. In simple words, you can use tuples as dictionary keys while you cannot use lists as dictionary keys. Let’s verify this.
my_list = ["a", "b"] my_dict = {my_list: "1"}
The above script creates a list with two items and then passes this list as a key for the only item in the dictionary. You should see the following error when you run the above script:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-14-bc7d34e7aa4e> in <module> 2 my_tuple = ("a", "b") 3 ----> 4 my_dict = {my_list: "1"} TypeError: unhashable type: 'list'
The error clearly says that the list is unhashable.
Let’s now try to create a tuple and pass it as a dictionary key.
my_tuple = (“a”, “b”)
my_dict = {my_tuple: “1”}
You will not see any error once you run the above script which means that tuples are hashable and can be used as dictionary keys.
Copied vs Reused
Since tuples are mutable, they cannot be copied. On the other hand, you can copy list items to a new list. Let’s see this with the help of an example. In the following script, a list is copied into another list. Next the original list and the copied lists are compared using the “is” method.
cars_list = ["Honda", "Toyota", "Ford", "BWM"] copy_cars_list = list(cars_list) print(cars_list is copy_cars_list)
Output:
False
The output returns false which means that the original and copied lists are not the same.
Let’s now try to copy a tuple and see what happens. Run the following script:
cars_tuple = ("Honda", "Toyota", "Ford", "BWM") copy_cars_tuple = tuple(cars_tuple) print(cars_tuple is copy_cars_tuple)
Output:
True
The output shows that when you try to copy a tuple, the same tuple object is returned instead of storing tuple items in a new tuple.