List and tuple in python are two different data structures and both have their own advantages. The list is a sequence in python and it is mutable which means that you can change its value. A tuple is an immutable sequence of values that can be changed only if you are the owner of the tuple object. This article will briefly highlight the concept of list and tuple in python along with their differences.
What is a List in Python?
A list in python is a sequence of values. Looping over a list would create an iterator that is able to iterate over the values of the list. The values in the list can be changed by assigning a variable to it, which would create a new list altogether. The list is mutable which means that you can change its values.
Use of List:
A list can be used whenever you have a collection of data. It is frequently used in for loop for iteration. This loop would create an iterator which is able to iterate over all the values present in the list object. The values in the list can be changed by assigning a variable to it. This would create a new list altogether with the new variable assigned to it.
Advantages of List:
List has following advantages over other containers such as dictionary and set:
1) It maintains its order automatically while traversing it.
2) Its length can be changed and it keeps on increasing.
3) Start from 0 and count from 1.
What is Tuple in Python?
Tuple is an immutable sequence of values. It cannot be changed once it has been created. You cannot assign values to variables once it has been assigned to a variable. A tuple looks like this (abc).
Use of Tuple in Python:
Tuple is a sequence of immutable values which cannot be changed once they are assigned to it. You cannot add or remove values while creating a tuple in python. Tuples have advantages over list in terms of immutability, it is easy to maintain the order of the objects and you can sort them alphabetically as well.
Advantages of Tuple:
Tuple has following advantages over other containers such as dictionary and set:
1) It maintains its order automatically while traversing it.
2) Its length can be changed and it keeps on increasing.
3) Start from 0 and count from 1.
Difference between List and Tuple in Python:
1) The most obvious difference between list and tuple in python is that list is a mutable data structure whereas tuple is an immutable one.
2) Tuple has fixed size, you cannot insert or remove objects from a tuple after the creation only exception being the prepending feature available with python 3 if you are a tuple owner that is.
3) Tuple cannot be converted to a list.
4) Tuple consists of heterogeneous values, whereas list consists of homogeneous values.
5) Tuples are heavily used in places where lists are not available or they are not feasible like OOP programming and core python modules.
6) You can sort the tuples alphabetically either using python built-in function or using the sorted() function provided by module “collections”.
7) A tuple can be iterated over directly whereas you need to use a for loop for iterating over a list in python.
8) A tuple can be nested inside another tuple, whereas a list cannot contain any more lists within it.
9) It is easy to maintain the order of the elements in a tuple.
10) Tuples can be passed as parameters to functions, whereas lists cannot be used for this purpose.
11) Tuples are more memory intensive than lists.
12) A tuple does not occupy as much storage space as a list does, and hence, it is more efficient than a list.
13) You can use tuples to create structured data using methods and attributes like dictionary dictionary_as_tuple() and set constructors. Using this functionality tuples are used in python to create dictionaries and sets that are immutable forms of dictionaries and sets respectively.
Conclusion:
Tuple is an immutable data structure, we cannot change the objects that are stored in a tuple after they are assigned to it. This makes them more memory efficient and easier to maintain. It is also easier to sort your tuples. Tuple allows you to pass values as parameters without having to convert them first into a list form. Lists on the other hand allow you to remove elements from the end of the list, but this is not possible with tuples. You can sort tuples using python built-in function or using the sorted() function provided by module “collections”. List can be converted into a tuple but you cannot use that flexibility with tuples, they are immutable and cannot be changed once they have been created.