What is the difference between a list and a tuple in Python?
The main difference is that a list is mutable (you can change it) and a tuple is immutable (you cannot change it after creating it).
| Point | List | Tuple |
|---|---|---|
| Brackets | [ ] | ( ) |
| Can change items | Yes | No |
| Speed and memory | A little slower, uses more memory | A little faster, uses less memory |
| Methods | Many, like append() and remove() | Only count() and index() |
| Use as dictionary key | No | Yes, if it holds only immutable items |
my_list = [1, 2, 3]
my_list[0] = 10
print(my_list)
my_tuple = (1, 2, 3)
my_tuple[0] = 10
Output:
Tutorials Class - Output Window
[10, 2, 3]
TypeError: 'tuple' object does not support item assignment
Use a list when the data will change, and a tuple when it should stay fixed. Read more about lists and tuples.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |