Intermediatecomparison
List vs tuple: what is the difference and when do you use each?
Source: Python tutorial: data structures
Quick answer
A list is mutable and meant for a changing collection; a tuple is immutable, so it is hashable when its contents are and can be a dictionary key or a set member.
A list is mutable and meant for a changing collection; a tuple is immutable, so it is hashable when its contents are and can be a dictionary key or a set member. Use tuples for fixed records (a composite key, a row) and lists when you append or modify. Tuples are slightly lighter, but the main reason to choose one is mutability.
Animated explanation
list
- Mutable
- Append and modify
- Not hashable
tuple
- Immutable
- Hashable if its contents are
- Fixed records, composite keys
Key points
- mutable vs immutable
- hashable keys
- fixed records