What is the difference between a Python Tuple and Python List?
![difference between tuple and list in python](https://pythonpoint.net/wp-content/uploads/2020/11/what-is-the-difference-between-a-python-tuple-and-python-list_-1024x576.jpg)
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")
Example: List
color = ["red", "green", "blue"] color[1]="black" print(color)
Output:
['red', 'black', 'blue']
Example: Tuple
color = ("red", "green", "blue") color[1]="black" print(color)
Output: Error
Traceback (most recent call last): File "test.py", line 3, in <module> color[1]="black" TypeError: 'tuple' object does not support item assignment
Subscribe
Login
Please login to comment
0 Discussion