Python | Difference Between List and Tuple
List and Tuple in Python are the class of data structure. The list is dynamic, whereas the tuple has static characteristics.
List is just like the arrays, declared in other languages. Lists need not be homogeneous always which makes it the most powerful tool in Python. In Python, the list is a type of container in Data Structures, which is used to store multiple data at the same time. Lists are a useful tool for preserving a sequence of data and further iterating over it.
Syntax:
Tuple is also a sequence data type that can contain elements of different data types, but these are immutable in nature. In other words, a tuple is a collection of Python objects separated by commas. The tuple is faster than the list because of static in nature.
Syntax:
Difference Between List and Tuple in Python:
1 | Lists are mutable | Tuples are immutable |
2 | Implication of iterations is Time-consuming | The implication of iterations is comparatively Faster |
3 | The list is better for performing operations, such as insertion and deletion. | Tuple data type is appropriate for accessing the elements |
4 | Lists consume more memory | Tuple consume less memory as compared to the list |
5 | Lists have several built-in methods | Tuple does not have many built-in methods. |
6 | The unexpected changes and errors are more likely to occur | In tuple, it is hard to take place. |
Python Lists Vs Tuples
In this article we will learn key differences between the List and Tuples and how to use these two data structure.
Lists and Tuples store one or more objects or values in a specific order. The objects stored in a list or tuple can be of any type including the nothing type defined by the None Keyword.
Lists and Tuples are similar in most context but there are some differences which we are going to find in this article.
The Key Difference between a List and a Tuple
The main difference between lists and tuples is the fact that lists are mutable whereas tuples are immutable.
What does that even mean, you say?
A mutable data type means that a python object of this type can be modified.
An immutable object can’t.
Let’s see what this means in action.
Let’s create a list and assign it to a variable.
>>> a = ["apples", "bananas", "oranges"]Now let’s see what happens when we try to modify the first item of the list.
Let’s change “apples” to “berries”.
>>> a[0] = "berries" >>> a ['berries', 'bananas', 'oranges']Perfect! the first item of a has changed.
Now, what if we want to try the same thing with a tuple instead of a list? Let’s see.
>>> a = ["apples", "bananas", "oranges"] >>> a[0] = "berries" Traceback [most recent call last]: File "", line 1, in TypeError: 'tuple' object does not support item assignmentWe get an error saying that a tuple object doesn’t support item assignment.
The reason we get this error is because tuple objects, unlike lists, are immutable which means you can’t modify a tuple object after it’s created.
But you might be thinking, Karim, my man, I know you say you can’t do assignments the way you wrote it but how about this, doesn’t the following code modify a?
>>> a = ["apples", "bananas", "oranges"] >>> a = ["berries", "bananas", "oranges"] >>> a ['berries', 'bananas', 'oranges']Fair question!
Let’s see, are we actually modifying the first item in tuple a with the code above?
The answer is No, absolutely not.
To understand why, you first have to understand the difference between a variable and a python object.
Difference between List and Tuples in Python
Contents
- 1 Difference between List and Tuples in Python
- 1.1 Comparison between List and Tuples in Tabular Form
- 1.2 Comparison Chart
- 1.3 List
- 1.3.1 Example of List:
- 1.4 Tuples
- 1.4.1 Syntax of Tuples
- 1.4.2 Example of Tuples
- 2 More Difference
Comparison between List and Tuples in Tabular Form
- ListsandTuples are very important data structures in Python.
- The Key Difference between List and Tuples is that the List iswritten in square brackets [ ] while Tuples are written in round brackets [ ].
- Also, List is Mutable and Tuple is immutable.
- In Python List is just like an array.
Comparison Chart
1 | List is mutable | Tuples are immutable |
2 | List iteration is slower and time-consuming. | Tuples iteration is faster. |
3 | List is performed well in insertion and deletion operations. | Tuples are performed well in accessing elements operations. |
4 | List takes more memory | Tuples take less memory. |
5 | Lists are many built-in methods | Tuples are less in-built methods. |
6 | In List Error is more likely to happen. | In Tuples Error hardly takes place. |
7 | Lists can be used to store homogeneous and heterogeneous elements. | Tuples are used to store only heterogeneous elements. |
List
- Syntax of List:
- list_num = ['1', '2', '3, '4', '5']
Example of List:
- list_num = ['1', '2', '3','4','5'] print[list_num]
- Output: [‘1’, ‘2’, ‘3’, ‘4’, ‘5’]
Tuples
Syntax of Tuples
- tuple_num = ['1', '2', '3', '4', '5']
Example of Tuples
- tuple_num = ['1', '2', '3', '4', '5'] print[tuple_num]
- Output: [‘1’, ‘2’, ‘3’, ‘4’, ‘5’]