Python List Vs Tuple
In this tutorial, we will learn the important difference between the list and tuples and how both are playing a significant role in Python.
Lists and Tuples are used to store one or more Python objects or data-types sequentially. Both can store any data such as integer, float, string, and dictionary. Lists and Tuples are similar in most factors but here we will describe the main difference between them.
Let's discuss the main differences in the following points.
Representation Differences
The representation of the Lists and tuple is marginally different. List are commonly enclosed with the square bracket [], and elements are comma-separated element. Tuples are enclosed with parenthesis (), and elements are separated by the comma. The parenthesis is optional to use, and these types of tuples are called tuple packing.
Consider the following example.
Output:
<class 'list'> <class 'tuple'>
In the above program, we defined a list1 variable which holds a list of different data type from index 0 to 4. We defined another variable tuple1, which holds a tuple of different data types. It is enclosed by the ().
Mutable Lists and Immutable Tuples
It is the most important difference between list and tuple whereas lists are mutable, and tuples are immutable. The lists are mutable which means the Python object can be modified after creation, whereas tuples can't be modified after creation. Consider the given an example.
Output:
['Peter', 'Joseph', 'Mathew', 'Ricky']
Now we are changing 0th index element "Peter" to "Samson".
Output:
['Samson', 'Joseph', 'Mathew', 'Ricky']
Now we create a tuple and do the same thing.
Output:
(10, 20, 'JavaTpoint', 30, 40)
Output:
TypeError Traceback (most recent call last) <ipython-input-5-52b2981fae12> in <module> ----> 1 a[0] = 50 TypeError: 'tuple' object does not support item assignment
We get an error while changing the 1st element of the tuple because of immutability. It does not support item assignment.
Debugging
The tuples are easy to debug in a big project because of its immutability. If we have a small project or less number of data, then lists play an effective role. Let's consider the following example:
Output:
[6, 9, 4, 'JavaToint', 7, 0, 1]
In the above code, we did b = a; here we are not copying the list object from b to a. The b referred to the address of the list a. It means if we make the change in the b then that will reflect the same as in list a, and it makes debugging easy. But it is hard for the significant project where Python objects may have multiple references.
It will be very complicated to track those changes in lists but immutable object tuple can't change after created.
So tuples are easy to debug.
Functions Support
The tuples support less operation than the list. The inbuilt dir(object) is used to get all the supported functions for the list and tuple.
- List Functions
Output:
['__add__','__class__','__contains__','__delattr__','__delitem__','__dir_, '__doc__','__eq__','__format__', '__get__','__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']
- Tuple Functions
Output:
['__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']
Memory Efficient
The tuples are more memory efficient than the list because tuple has less built-in operations. Lists are suitable for the fewer elements whereas tuples are a bit faster than the list for the huge amount of data.
Output:
Tuple size = 168 List size = 216
Conclusion
- In Some cases, lists might seem more useful than tuples. But tuples are important data structures of the Python. Tuples are commonly used for unchangeable data or we can say that the data will be "write- protected" in the tuples. Tuples sends the indication to the Python interpreter the data should not change in the future.
- We can use tuple the same as a dictionary without using keys to store the data. For example-
- Tuples can use for the dictionary keys because these are hashable and immutable whereas lists can't use a keys in dictionary.
0 comments:
Post a Comment
Thanks