What is the difference between list and tuples in Python?
LIST
Type
- Are mutable (can be edited), it is possible update or delete an item from the list.
Iteration
- It is slower than tuples.
Syntax
- You use square bracket.
Example:
supermarket_list = ["Cereal", "bread", "avocado", "beef"]
print(supermarket_list)
# Output:
# ['Cereal', 'bread', 'avocado', 'beef']
TUPLES
Type
- Are immutable, if you try to modify as a result you will have an error. For example if you try to delete some item you will get an error instead yu have to assign it to a new Tuple.
Iteration
- It is faster than list.
Syntax
- You use round brackets.
Example:
supermarket_list = ("Cereal", "bread", "avocado", "beef")
print(tuple)
# Output:
# ('Cereal', 'bread', 'avocado', 'beef')
References
How to add Elements to a List in Python