Collections
14 min
Python has four built-in collection types, each with a different shape:
| Type | Ordered | Mutable | Duplicates | Syntax |
|---|---|---|---|---|
| list | yes | yes | allowed | [1, 2, 3] |
| tuple | yes | no | allowed | (1, 2, 3) |
| set | no | yes | not allowed | {1, 2, 3} |
| dict | yes (3.7+) | yes | keys unique | {"a": 1} |
tuple's immutability makes it usable as a dict key or set element —
something a list can never do, since dict keys and set elements must be
hashable, and mutable objects generally aren't.
Sets support the same operators as mathematical set theory — union, intersection, and difference are all O(min(len(a), len(b))) on average, much faster than nested loops over lists.
Reach for each one based on what you actually need:
- list — an ordered sequence you'll iterate, index, or append to.
- tuple — a fixed, small group of values (like coordinates
(x, y)), or anything you need to use as a dict key / set element. - set — you only care about membership ("is x in here?") and
uniqueness, not order —
inis O(1) average vs O(n) for a list. - dict — you're mapping keys to values, or counting/grouping things.
Write a function `unique_preserve_order(items)` that returns a list of the unique elements in `items`, keeping the order of their first occurrence.
Write a function `common_elements(a, b)` that returns a sorted list of the elements present in both `a` and `b`. Use set operations, not nested loops.
Which Python built-in collection type is both ordered and immutable?
You know when to reach for list vs tuple vs set vs dict, and can use set operations for membership/intersection problems.