list vs tuple vs deque vs array — which and why?
Pick by the operation you do most. list for ordered mutable data with random access; tuple when it must not change or must be a dict key; deque when you touch the front; array when you have millions of numbers; set when you only ask "is it in there?".
Overview
tuple versus list
The textbook answer is "tuples are immutable", which is true and not the point. The consequence is that a tuple is hashable (if its contents are), so it can be a dictionary key or a set member — which is why coordinates, database rows and cache keys are tuples.
The secondary signal is meaning. A list is a homogeneous sequence of unknown length; a tuple is a fixed-size record where position carries meaning. (x, y) is a point; [x, y] is two numbers.
Step through it
What to watch
- The front of a list is the trap — O(n), where a deque is O(1).
- Hashability is the real tuple/list distinction, not immutability for its own sake.
- An array stores values; a list stores references to them.
Say this out loud
"Tuple if it's fixed or needs to be hashable - it can be a dict key, a list can't. deque if I'm touching the front, because list.pop(0) is O(n). array or numpy for large numeric data, since a list stores pointers rather than values."