What is the difference between a Python Tuple and Python List?
Lists and tuples are ordered sequences of objects.
The main difference between a python tuple and a python list is that the former is immutable while the latter is mutable. i.e. we can change/modify the values of a list but we cannot change/modify the values of a tuple.
- Processing of a tuple is faster than list.
- Iterations in tuples are faster than iterations in lists.
- Lists consume more memory than tuples.
- Lists have many built-in methods, while tuples does not have any built-in methods
Syntax of List:
color = ["red", "green", "blue"]
Syntax of Tuple:
color = ("red", "green", "blue")
color = ["red", "green", "blue"] color="black" print(color)
['red', 'black', 'blue']
color = ("red", "green", "blue") color="black" print(color)
Traceback (most recent call last): File "test.py", line 3, in <module> color="black" TypeError: 'tuple' object does not support item assignment
Please login to comment
View all comments