What is the difference between Tuple & list

The main difference between lists and a tuples is that lists are mutable whereas tuples are immutable.

For Example:-

if we modify data in tuple then it will generate error

>>> a = (23, 23, 45)
>>> print(a)
(23, 23, 45)
>>> a[1] = 77
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

 

Whereas data in list can be modified, example-

>>> a = [45, 46, 89, 90]
>>> print(a)
[45, 46, 89, 90]
>>> a[2] = 2345
>>> print(a)
[45, 46, 2345, 90]

 

Total Views: 454