Compare Python's four main built-in collections. Give the typical time cost of a membership test (x in c) for each, and a situation where each is the right choice.
Show the explanation
list | tuple | set | dict | |
|---|---|---|---|---|
| Syntax | [1, 2] | (1, 2) | {1, 2} (empty: set()) | {"a": 1} (empty: {}) |
| Ordered? | yes | yes | no | keeps insertion order (guaranteed since 3.7) |
| Mutable? | yes | no | yes | yes |
| Duplicates? | allowed | allowed | no | keys unique |
x in c | O(n) scan | O(n) scan | O(1) average (hash) | O(1) average, on keys |
| Hashable (usable as a dict key)? | no | yes, if all its items are | no (frozenset is) | no |
When to use which
- list: an ordered sequence you will append to, sort or index: marks in exam order, a queue of tasks. (For a real queue,
collections.dequegives O(1) pops from the left.) - tuple: a fixed record of a few related values, such as
(lat, lng)or(roll, name), returning several values from a function, or a dict key made of several parts, as inseat[(row, col)]. - set: fast membership tests and removing duplicates:
seen = set(),banned_words, and set algebra with&,|,-. - dict: look a value up by a key: roll number to student, word to count.
Common follow-ups
{}is an empty dict, not a set.- A one-element tuple needs a comma:
(5,).(5)is just the number 5. - A tuple is immutable, but if it holds a list, that list can still change:
t = ([1],); t[0].append(2)works. - Sets and dicts need hashable elements or keys. That is why a list cannot be a key.
Original UniCircle question. Last reviewed 22 Oct 2026.
