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.
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.
<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.
['Peter', 'Joseph', 'Mathew', 'Ricky']
Now we are changing 0th index element "Peter" to "Samson".
['Samson', 'Joseph', 'Mathew', 'Ricky']
Now we create a tuple and do the same thing.
(10, 20, 'JavaTpoint', 30, 40)
TypeError Traceback (most recent call last) <ipython-input-5-52b2981fae12> in <module> ----> 1 a = 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.
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:
[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.
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.
['__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']
['__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']
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.
Tuple size = 168 List size = 216