Overview
Curated: · Written: · Reviewed:
Core Data Structures
In one line
Lists, tuples, dicts and sets are containers of references. Choose each by its mutability, ordering, uniqueness and lookup cost: a list for ordered mutable sequences, a dict for keyed lookup, a set or frozenset for membership. Language guarantees are stated as such below; CPython details are marked as implementation behaviour. Current to Python 3.14.
Key definitions
- Aliasing: two names bind one object. Assignment copies no data.
- Shallow copy: a new outer container holding the same nested references (
a.copy(),a[:]). - Hashable: has a hash that stays fixed for its lifetime and an equality consistent with that hash.
- Equality class: the values a set treats as one element.
- Dict view: a live window onto
keys(),values()oritems(), not a snapshot. - Sentinel: a private object that tells absent apart from a stored
None.
Formulas and complexity
| Operation | list | deque | dict / set |
|---|---|---|---|
x in c | O(n) | O(n) | O(1) average |
| append / pop at right | O(1) amortized | O(1) | n/a |
| insert / pop at left | O(n) | O(1) | n/a |
c[i] | O(1) | O(n) near middle | n/a |
How it works
- A list or tuple stores references. A tuple's slots are fixed, but a list inside it can still change.
- Lookup in a dict or set: the hash picks a starting slot, probing walks candidates, and
__eq__confirms the match. Collisions are resolved by equality, never by the hash alone. - A deleted slot keeps a dummy marker so later probe chains pass through it. Dummies count toward the fill that triggers a resize, so a delete-heavy table gets rebuilt on a later insert (CPython behaviour).
- Dicts keep insertion order (language guarantee since 3.7). Sets make no order promise.
When to use and when not to
| Need | Choose | Not when |
|---|---|---|
| Ordered, mutable, indexed sequence | list | you need a fixed record |
| Fixed record usable as a dict key | tuple of hashable members | any member is a list or dict |
| Keyed lookup that keeps insertion order | dict | you need no values |
| Repeated membership tests | set or frozenset | a list scanned in a loop |
| Ordered uniqueness | dict.fromkeys(...) | a set, which promises no order |
| FIFO or both-end operations | deque | you index or slice the middle |
| Stable value key for a permission set | frozenset | members are mutable |
Traps interviewers probe
b = ais not a copy. Both names change together. Fix:a.copy()or a comment saying the sharing is intended.[[0] * w] * hshares one row. Fix:[[0] * w for _ in range(h)].def f(items=[])keeps state across calls. The default is built once at definition time. Fix: default toNone, then allocate inside the function.if not valuetreatsNone,[]and0alike. Fix: testis None, or use a private sentinel withget(name, sentinel).- A tuple holding a list is treated as hashable. The tuple itself fails
hash(). Fix: check every nested member, or normalize to a frozenset or tuple of tuples.
Interview tips
Open by naming the contract the data needs (ordering, uniqueness, mutability, dominant operation), then the container that meets it.
Follow-ups to expect: shallow versus copy.deepcopy; why in on a list is linear; how equality resolves hash collisions; why frozenset suits a cache key; why Counter keeps zero and negative counts and needs explicit deletion.
30-second outline: "The workload needs X and Y, so I use Z. Z gives O(1) average lookup because the hash locates a slot and equality confirms it. Its invariant is that the key's hash stays fixed. The trap I would check is aliasing or nested mutable state. I would profile before changing anything else."
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.