In this tutorial, let us look at the key differences between the list and tuple in python with examples.
List and tuple are data structures in Python that can store one or more objects or values. A list, which can be created with square brackets, is used to store multiple items in one variable. Similarly, tuples, which can be declared with parentheses, can store multiple items in a single variable.
The list has dynamic properties, whereas the tuple has static properties.
List
List objects are declared similarly to arrays in other programming languages. Lists do not have to be homogeneous all of the time, so they can store items of different data types at the same time. As a result, lists are the most useful tool. The list is a Python container data Structure that is used to hold multiple pieces of data at the same time. Lists come in handy when we need to iterate over some elements while keeping hold of the items.
Tuple
A Tuple is a sequence data type that can contain elements of various data types, but it is immutable. A tuple is simply a collection of Python objects separated by commas. Because tuples are static in nature, they are faster than lists.
Let us now look at the major differences between the list and tuple along with the examples.
Difference Between List and Tuple in Python
1)Syntax Differences
The syntax of the list and tuple differs slightly. Lists are denoted by square brackets [], while Tuples are denoted by parenthesis ().
# Give the list as static input and store it in a variable gvn_lst = [5, 8, 1, 2] # Give the tuple as static input and store it in a variable gvn_tuple = (5, 8, 1, 2) # print the given list print("The given list = ", gvn_lst) # print the given tuple print("The given tuple = ", gvn_tuple) # print the datatype of given list using the type() function print("The datatype of given list = ", type(gvn_lst)) # print the datatype of given list using the type() function print("The datatype of given list = ", type(gvn_tuple))
Output:
The given list = [5, 8, 1, 2] The given tuple = (5, 8, 1, 2) The datatype of given list = <class 'list'> The datatype of given list = <class 'tuple'>
2)List is Mutable where as Tuple is Immutable
A key difference between a list and a tuple is that lists are mutable whereas tuples are immutable. What does this mean exactly? It means that the items in a list can be changed or modified, whereas the items in a tuple cannot be changed or modified.
Because a list is mutable, we cannot use it as a dictionary key. This is due to the fact that a Python dictionary key is an immutable object. As a result, tuples can be used as dictionary keys if necessary.
# Give the list as static input and store it in a variable gvn_lst = [5, 8, 1, 2] # Give the tuple as static input and store it in a variable gvn_tuple = (5, 8, 1, 2) # Modifying the list element at the index 1 gvn_lst[1] = 10 # print the given list after modification print("The given list after modification = ", gvn_lst) # Modifying the tuple element at the index 1 # Here we get an error because as the tuple is immutable, the elements of a tuple cannot be changed gvn_tuple[1] = 10 print(gvn_tuple)
Output:
The given list after modification = [5, 10, 1, 2] --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-8-306436f9dc4b> in <module> 11 # Modifying the tuple element at the index 1 12 # Here we get an error because as the tuple is immutable, the elements of a tuple cannot be changed ---> 13 gvn_tuple[1] = 10 14 print(gvn_tuple) TypeError: 'tuple' object does not support item assignment
Explanation:
Here, we assigned 10 to gvn_lst at index 1 and found 10 at index 1 in the output. We also got a type error when we assigned 10 to gvn_tuple at index 1. We can't change the tuple because it's immutable.
3)Comparison in Size
Tuples operations have a smaller size than list operations, which makes them a little faster, but not by much unless you have a large number of elements.
# Give the list as static input and store it in a variable gvn_lst = [5, 8, 1, 2, 7, 8, 10, 15, 18, 20] # Give the tuple as static input and store it in a variable gvn_tuple = (5, 8, 1, 2, 7, 8, 10, 15, 18, 20) print('Given list size =', gvn_lst.__sizeof__()) print('Given tuple size =', gvn_tuple.__sizeof__())
Output:
Given list size = 120 Given tuple size = 104
4)Available Functions
Lists have more built-in functions than tuples. We can use the built-in function dir([object] to access all of the list and tuple’s methods.
The number of methods available in these two also differs, with tuples having 33 methods and lists having 46.
# Give the list as static input and store it in a variable gvn_lst = [5, 8, 1, 2] # Give the tuple as static input and store it in a variable gvn_tuple = (5, 8, 1, 2) print(dir(gvn_lst)) print(dir(gvn_tuple))
Output:
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
List vs Tuple
            List |             Tuple |
Mutable | Immutable |
In the list, the implication of iterations is time-consuming. | Iteration implications are much faster in tuples. |
The list performs better for operations like insertion and deletion. | Elements can be accessed better in tuples |
The Lists consume more memory | When compared to a list, a tuple consumes less memory. |
Have More built-in functions | Tuples have fewer built-in functions when compared to lists |
In lists, Unexpected changes and errors are more likely to occur. | Tuples rarely generate unexpected errors or changes. |
List vs Tuple Similarities
- Both lists and tuples hold collections of elements and are heterogeneous data types, which means they can hold multiple data types at the same time.
- They are both ordered, which means the items or objects will remain in the same order in which they were placed until manually changed.
- We can iterate through the objects they hold because they are both sequential data structures; thus, they are iterable.
- A square bracketed [index] integer index can be used to access objects of both data types.