Python is the most popular programming language of 2022. It comes with many powerful built-in data structures and data types. Python data structures allow their developers to store and organize data in different formats. This article goes into tuple vs list. Lists and tuples are the two most commonly used data structures in Python. Both data structures are capable of performing the same task, yet developers use them in different cases.
Here in this Python tutorial, we will learn about Python lists and Python tuples, and the main objective of this tutorial will be distinguishing between tuples and lists. Before we dive into the comparison of the Python list and Python tuple, let’s have a brief introduction to both the data structures.
What is a Python List?
A Python list is an inbuilt Python data structure that can store different data elements in sequential order. Python lists are mutable in nature, which means once we define a list with some elements, we can change those elements throughout the program. To declare a list in Python, we use square brackets and place all the elements or items inside them, separated by a comma.
>>> numbers = [1, 12, 34, 56, 67] >>> print(numbers) [1, 12, 34, 56, 67]
In the Python list, every element is associated with an index value that starts from
0
and goes up to
n-1
, where
n
is the total number of elements present in the list. For instance, the index value of the first element will be 0. It will be 1 for the second element, and so on.
>>> numbers[0] 1 >>> numbers[1] 12 >>> numbers[2] 34
A list can store heterogeneous elements, which means we can have elements of different data types in a list.
>>> hetro_list = ['1', 'one', 1, 1.0, True] >>> print(hetro_list) ['1', 'one', 1, 1.0, True]
Lists are mutable. Therefore, we can change the elements of a Python list after the declaration.
>>> my_list = ['pen', 'boat', 'paper', 'pencil'] >>> my_list ['pen', 'boat', 'paper', 'pencil'] >>> my_list[1] = 'book' >>> my_list ['pen', 'book', 'paper', 'pencil']
What is a Python Tuple?
Similar to the Python list, the Python tuple is also a Python built-in data structure that stores elements in sequential order. However, unlike Python lists, Python tuples are immutable. This means that once we declare the elements for a tuple, we can not change them. To declare a tuple in Python, we use parentheses and place all the elements inside them separated by a comma.
>>> numbers = (1, 12, 23, 34, 45, 56) >>> print(numbers) (1, 12, 23, 34, 45, 56) >>> print(type(numbers)) <class 'tuple'>
Similar to the Python list, we can also use indexing and slicing to access the individual and a particular sequence of elements, respectively in a Python tuple.
>>> numbers = (1, 12, 23, 34, 45, 56) >>> numbers[0] 1 >>> numbers[0:4] (1, 12, 23, 34)
Python tuple can also store heterogeneous elements.
>>> hetro_tuple = (1, '1', 1.0, "One", True) >>> hetro_tuple (1, '1', 1.0, 'One', True) >>> hetro_tuple[2] 1.0
Tuples are immutable data structures. This means that once elements of a tuple are declared, we cannot change them. If we try to do so, we will receive an error.
>>> my_tuple = ('pen', 'boat', 'paper', 'pencil') >>> my_tuple[1] 'boat' >>> my_tuple[1] = 'book' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment
Difference Between Tuple and List in Python (Tuple vs List)
From the above sections detailing Python lists and Python tuples, you would have got the basic difference between a tuple and a list in Python. There is only one major difference between a Python list and a Python tuple. While lists are mutable data structures, tuples are immutable data structures. Apart from this difference, list and tuple have the same working and principles. Nonetheless, there are a few differences among the two that are discussed in the following table:
Tuple vs List: A Head-to-Head Comparison Table
Python List |
Python Tuple |
To declare a Python list, we use square brackets.
>>> my_list = [1,2,3,4,5,6] >>> type(my_list) <class 'list'> |
To declare a Python tuple, we use parentheses.
>>> my_tuple = (1,2,3,4,5,6) >>> type(my_tuple) <class 'tuple'> |
Lists are mutable data structures. Elements of a list can be changed after the declaration.
>>> my_list = [1,2,3,4,5,6]
>>> my_list[0] = 10
>>> my_list
[10, 2, 3, 4, 5, 6]
|
Tuples are immutable data structures. The elements of a tuple cannot be changed after the declaration.
>>> my_tuple = (1,2,3,4,5,6)
>>> my_tuple[0]=10
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
|
Python lists support 12 built-in methods
. These are:
['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
|
Python tuples only support the following two methods:
['count', 'index']
|
By convention, developers use lists to store heterogeneous data types. | Python tuples are also capable of storing heterogeneous elements, but they are often used to store homogeneous elements, i.e., elements of the same data type. |
Conclusion
That completes this Python Tuple vs List tutorial. Here, you learned all the major differences between lists and tuples in Python. Generally, we use a Python list when we want a data structure for the read and write operations. On the other hand, Python tuples are used when we only want to perform the read operation on the data structure.
Both the data structures are capable of storing heterogeneous data elements, but by convention, lists are used for heterogeneous elements, and tuples are used for homogeneous elements.
People are also reading:
Leave a Comment on this Post