Difference between List and Tuple in Python
In this tutorial, we will discuss the fundamental difference between list and tuple in Python with the help of examples.
We are aware that Python is a commonly used programming language in the world of technology. It provides a vital set of data structures to make programming easier and more efficient.
Two of the most used data structures in Python are the list and the tuple. Both have many similarities, such as:
- Both data structures preserve insertion order.
- Both allow to store duplicate objects or elements.
- Heterogeneous objects are allowed in them.
- Both list and tuple support slicing and indexing.
- Both support the built-in functions, such as len(), count(), index(), min(), and max() to perform different operations.
Despite having many similarities, they also have some important dissimilarities.
In this tutorial, we will gain knowledge of all the important distinctions between lists vs tuples in Python. So, let’s start with the basic definition.
Difference between List and Tuple in Python
1. Definition of list and tuple:
- A list is a data structure in Python that represents a set of values or elements by a single name.
- On the other hand, a tuple is a data structure similar to the list but we cannot update it dynamically.
2. Mutable vs Immutable nature:
- The major difference between a list and tuple in Python is that a list object is mutable in nature, while a tuple object is immutable.
- It means that once we create a tuple object, we cannot update or modify the content of it. If we try to modify it, will raise an error. In contrast, we can modify or update elements of a list in the existing list object after creating it.
Example 1:
# Creating a list.
my_list = [2, 3, 8, 5, 6]
# Updating an element in the list.
my_list[2] = 4
print("List elements after updating: ", my_list)
# Creating a tuple.
my_tuple = (2, 3, 8, 5, 6)
# Trying to update an element in the tuple.
my_tuple[2] = 4
print(my_tuple)
Output: List elements after updating: [2, 3, 4, 5, 6] my_tuple[2] = 4 TypeError: 'tuple' object does not support item assignment
In this example, the generated error clearly tells that tuple objects cannot be mutated.
3. Syntax of list and tuple:
- A list is a group of comma-separated elements enclosed in square brackets [ ]. Square brackets are mandatory in the list. The basic syntax of creating a list in Python is as:
my_list = [element1, element2, element3, ...elementn]
# For example:
my_list = ["John", "Bob", "Amit", "Deep"]
- A tuple is a group of comma-separated elements is enclosed in parentheses (). Parentheses is optional in the tuple. The basic syntax of creating a tuple in Python is as:
my_tuple = (element1, element2, element3, ...elementn)
# For example:
my_tuple = ("John", "Merry", "Ricky", "Mark")
4. Memory consumption:
- A list is dynamic in nature. It shrinks and grows in accordance with elements inserted or deleted from the list.
- A tuple is also dynamic in nature, but we can allocate the memory to the tuple only once at the time of creation. It means that a tuple cannot shrink or grow according to elements inserted or deleted from the tuple.
5. Speed:
- A list in Python is mutable in nature and takes more time to iterate over a list.
- A tuple in Python is immutable in nature and takes less time to iterate over a tuple.
6. Performance:
- A list object is dynamic in nature, but if the need of memory occurs at runtime, the operating system needs to allocate memory to the list. This degrades the performance of the list because OS needs time to allocate the memory to it.
- On the other hand, since we assign elements to the tuple only once, therefore, it is not required to allocate memory at run time. This improves the performance of a tuple as comparison to list.
7. Flexibility:
- A list object provides the flexibility feature. Therefore, we can execute any number of elements in the list without changing the size of list.
- A tuple object is not flexible in nature. As tuple allows to store elements only once, there is no method to update elements stored in the tuple.
Example:
# Creating a list.
my_list = ["Mango", "Orange", "Banana"]
# Adding two new elements in the list.
my_list.append("Grapes")
my_list.append("Guava")
# Displaying list after adding two new elements.
print(my_list)
Output: ['Mango', 'Orange', 'Banana', 'Grapes', 'Guava']
8. Usage for dictionaries:
- We can use a list object as a value for a dictionary because the values of a dictionary are mutable in nature. However, it cannot becomes keys of the dictionary.
- On the other hand, a tuple object can become a key for the dictionary because keys of the dictionary are immutable in nature.
Example:
# Creating a dictionary containing lists as values for keys.
dic = {1: [2, 3, 4], 2: [5, 6, 7]}
# Displaying the values of keys.
print(dic[1])
print(dic[2])
# Creating a dictionary containing tuples as keys.
dic = {(1, 2,): "John", (3, 4): "Amit"}
# Displaying the values of keys.
print(dic[1, 2])
print(dic[3, 4])
Output: [2, 3, 4] [5, 6, 7] John Amit
9. Methods and functions availability:
- A list provides various types of methods or functions by which we can easily perform a wide range of list operations without writing long code.
- On the other hand, a tuple provides lesser methods or functions. We need to write our own code in the tuple to perform certain operations.
10. Uses:
- We should go for a list when element values in the list are not fixed and keep on frequently changing.
- In contrast, if the element values are fixed and will never changes, we should go for a tuple.
In this tutorial, you have learned about the difference between list and tuple in Python with the help of examples. Hope that you will have understood the basic difference between list vs tuple and enjoyed this tutorial. In the next, we will get the knowledge of set in Python and how to create it.
If you like this tutorial and useful for you, please share it on the social networking sites for your friends.
Thanks for reading!!!