Lists vs Tuples vs Sets vs Dicts in Python (When to Use Each)
Python's four built-in collections compared: lists are ordered and mutable, tuples are immutable, sets hold unique items, dicts map keys to values. Here's when to use each, with a cheat table and the interview gotchas.
Python gives you four built-in collections, and choosing the right one is a real skill: lists are ordered and changeable, tuples are ordered but fixed, sets hold unique items with instant lookup, and dicts map keys to values. Pick wrong and your code is slower or buggier; pick right and the problem half-solves itself.
This is foundational Python and a favourite interview topic — especially “list vs tuple” and “why can a tuple be a dict key?”. Here’s each one, when to reach for it, and the gotchas.
The four at a glance
| List | Tuple | Set | Dict | |
|---|---|---|---|---|
| Syntax | [1, 2, 3] | (1, 2, 3) | {1, 2, 3} | {"a": 1} |
| Ordered | yes | yes | no* | yes (3.7+) |
| Mutable | yes | no | yes | yes |
| Duplicates | yes | yes | no | keys unique |
| Lookup | by index O(1) | by index O(1) | membership O(1) | by key O(1) |
*Sets don’t guarantee any order; never rely on it.
Lists — the default, changeable collection
A list is an ordered, mutable sequence — the structure you’ll reach for most.
todos = ["write", "test", "ship"]
todos.append("deploy") # add
todos[0] = "design" # change
todos.remove("test") # delete
Use a list when the collection changes over time, order matters, and duplicates are allowed — a queue of tasks, rows from a file, results you’re accumulating.
Tuples — fixed, lightweight records
A tuple is an ordered but immutable sequence. Once created, it can’t change.
point = (4, 5)
# point[0] = 9 → TypeError: tuples are immutable
x, y = point # unpacking
Use a tuple when the data is fixed and meaningful as a unit — coordinates, RGB colours, a function returning multiple values, or a record you don’t want mutated. Immutability is a feature: it signals intent and prevents accidental changes.
Sets — unique items, instant membership
A set is an unordered collection of unique items, built for fast “is this in here?” checks and de-duplication.
seen = {1, 2, 3}
seen.add(2) # already there → no change
print(4 in seen) # O(1) membership test
unique = set([1, 1, 2, 3]) # → {1, 2, 3}, duplicates gone
Use a set when you need uniqueness or fast membership tests, or set algebra:
a, b = {1, 2, 3}, {2, 3, 4}
a & b # intersection → {2, 3}
a | b # union → {1, 2, 3, 4}
a - b # difference → {1}
Replacing an x in my_list check inside a loop with a set turns an O(n²) pattern into O(n) — a common optimisation, and the same idea behind hash maps in coding interviews.
Dicts — key → value, the workhorse
A dict maps unique keys to values with O(1) lookup. It’s Python’s most important structure after the list.
user = {"name": "Ana", "active": True}
user["age"] = 30 # add / update
print(user.get("email", "—")) # safe access with default
for key, value in user.items():
print(key, value)
Use a dict when you need to look something up by a meaningful key — counting occurrences, grouping, caching, or representing a record. Since Python 3.7 dicts keep insertion order, so you get fast lookup and predictable iteration.
Which one? A quick decision guide
- Changing, ordered, duplicates OK → list
- Fixed set of values, won’t change → tuple
- Uniqueness or fast membership → set
- Look up values by a key → dict
Common mistakes
- The mutable default argument trap.
def f(items=[])shares one list across all calls. Usedef f(items=None): items = items or []. - Copying.
b = acopies the reference, not the list — changebandachanges too. Usea.copy()orlist(a). For nested structures,copy.deepcopy. - Relying on set order. Sets are unordered; if you print one, don’t trust the arrangement.
- Using a list where a set belongs. Repeated
inchecks on a large list are O(n) each — switch to a set.
a = [1, 2]; b = a; b.append(3) leaves a as [1, 2, 3] too — both names point to the same list. This catches every Python beginner. Copy explicitly when you want independence.
Where this fits
These four structures are Phase 2 of our Python roadmap, and they underpin everything after — classes store data in them, and decorators often cache results in dicts.
Each is drawn out with diagrams and worked examples in Python in One Week (the fast refresher) and Python in One Month (the full beginner path); the interview angle — hashing, time complexity, when each wins — is in Python in Three Months. For how these collections behave in memory, mutability and identity (is vs ==), and CPython internals, Python for Staff Engineers goes deeper.
Learn what each collection is good at, and the right one becomes obvious.
Frequently asked questions
What is the difference between a list and a tuple in Python?
A list is mutable — you can add, remove, and change items — and is written with square brackets: [1, 2, 3]. A tuple is immutable — fixed once created — and written with parentheses: (1, 2, 3). Use a list for a changing collection and a tuple for fixed data that should not change, like a coordinate.
Why can a tuple be a dictionary key but a list cannot?
Dictionary keys must be hashable, and hashability requires immutability. Tuples are immutable so they can be hashed and used as keys; lists are mutable so they cannot. This is the most common interview question about Python collections.
When should I use a set?
Use a set when you need unique items and fast membership tests. Checking 'is x in this collection?' is O(1) on average for a set versus O(n) for a list. Sets are also perfect for removing duplicates and for union/intersection operations.
Are dictionaries ordered in Python?
Yes, since Python 3.7 dictionaries preserve insertion order as a language guarantee. They remain optimised for key lookup, not positional access — you still retrieve values by key, not by index.