Dictionaries & Sets: Instant Lookup
Dictionaries and sets are the secret weapons of fast code. Thanks to hashing, they find things almost instantly โ turning slow scans into direct lookups.
If lists are the workhorse, dictionaries and sets are the secret weapons. Both use a technique called hashing to find items almost instantly, no matter how much data you have - the opposite of a list's slow scan. Learning to reach for a dict or set at the right moment is one of the most practical skills in this whole course, and it makes a huge difference to real code.
Hashing = near-instant lookup
A dictionary maps keys to values, and a set holds unique items - and both can check 'is this here?' or 'what's the value for this key?' in roughly constant time, however big they get.
| Structure | For | Lookup speed |
|---|---|---|
| dict {key: value} | Look up a value by its key | Near-instant |
| set {items} | Unique items + fast membership | Near-instant |
| list [items] | Ordered data (by contrast) | Slow scan to find |
The magic is hashing: the structure computes where an item should live from the item itself, so it can jump straight there instead of searching. That's why 'x in myset' is fast even for millions of items.
When to reach for them
Use a dict when you look data up by a name/key; use a set when you need uniqueness or fast membership checks.
# count words with a dict (key -> count)
counts = {}
for word in text.split():
counts[word] = counts.get(word, 0) + 1
# fast membership + de-duplication with a set
seen = set()
for item in stream:
if item in seen: # near-instant, no scan
continue
seen.add(item)Look things up fast
- Dict - map keys to values; look up by key near-instantly.
- Set - unique items with fast membership and easy de-duplication.
- Hashing is why both are fast - they compute where data lives.
- Replace list scans with a set/dict whenever you look things up by value/key.
๐ฏ Key takeaways
- Dictionaries (key->value) and sets (unique items) use hashing for near-instant lookup, however big.
- This is the opposite of a list's slow scan - the magic is computing where an item lives.
- Use a dict to look up by key (like counting words); use a set for uniqueness and fast membership.
- The top beginner speed win: replace repeated 'x in list' scans with a set or dict.
Frequently asked
What is 'hashing' and why does it make lookups so fast?
Hashing is computing a number (a 'hash') from an item, which the structure uses to decide where to store it. The clever part: because the location is derived from the item itself, when you later ask 'is this item here?' or 'what's the value for this key?', the structure recomputes the hash and jumps straight to where it would be - instead of searching through everything. That's why a set or dictionary can check membership or fetch a value in roughly constant time no matter how many items it holds, while a list has to scan. You don't need to understand the maths of hash functions to use them; just know that dicts and sets 'know where to look' thanks to hashing, which is what makes them fast.
If sets and dicts are so fast, why not always use them instead of lists?
Because they trade away things lists provide. Sets and dictionaries don't give you position-based access (there's no 'the third item' in a set the way there is in a list), sets don't allow duplicates, and while modern Python dicts preserve insertion order, sets are conceptually unordered. Lists are the right choice when order and position matter, when you need duplicates, or when you're iterating in sequence. The point isn't that one structure is best - it's matching the structure to the job: lists for ordered/positional data, sets for uniqueness and membership, dicts for key-based lookup. Great programmers use all three, each where it fits, and often combine them.
Related lessons
Lists & Arrays: Ordered Data
The list is the workhorse data structure โ an ordered sequence you can index, slice and grโฆ
Data Structures & AlgorithmsStacks, Queues & Beyond
Some structures aren't about storage but about *order of access* โ stacks (last-in-first-oโฆ
Python Fundamentals (Hands-On)Lists and Dictionaries
Real programs juggle lots of data. Lists (ordered collections) and dictionaries (labelled โฆ