Follow one key through hash, bucket and chain — then watch a set intersect two lists in O(1) probes per item.
The big picture
Meet the hash table
A dict finds any value in one hop: the key is hashed, the hash picks a bucket, and the walk is short. That trick powers half of Python problem solving.
1
The hash table
dict is a hash table: key → hash → bucket → value. Average lookup, insert and delete are all O(1).
2
Collisions happen
Two keys can land in one bucket. Python chains them — the walk gets longer, but lookups stay correct.
3
dict idioms
counts.get(ch, 0) + 1, "k" in d and d.items() — the everyday frequency-counting toolkit.
4
Sets = keys only
A set is a dict without values: unique items, O(1) membership, union |, intersection &.
5
Hashable rules
Keys must be immutable: strings, numbers and tuples qualify — lists and dicts are not hashable.
Meet the hash table
The hash table1dict is a hash table: key → hash → bucket → value. Average lookup, insert and delete are all O(1).
Collisions happen2Two keys can land in one bucket. Python chains them — the walk gets longer, but lookups stay correct.
dict idioms3counts.get(ch, 0) + 1, "k" in d and d.items() — the everyday frequency-counting toolkit.
Sets = keys only4A set is a dict without values: unique items, O(1) membership, union |, intersection &.
Hashable rules5Keys must be immutable: strings, numbers and tuples qualify — lists and dicts are not hashable.
5 building blocks make up Meet the hash table
Algorithm in motion
Buckets & collisions
Five buckets, seven keys — watch key % 5 assign homes and chaining absorb the pile-up in bucket 2.
—
bucket 0
—
bucket 1
—
bucket 2
—
bucket 3
—
bucket 4
1 / 10
A hash table with 5 buckets. A key’s home is key % 5 — hashing is just arithmetic.
How it works
One lookup, staged
What happens between d[key] and the answer — five stages, mostly arithmetic.
Watch it run
Walkthrough
A frequency counter built on dict.get — then the classic seen-dict that finds a pair in one pass.
freq.py
1word ="mammal"
2
3counts ={}
4for ch in word:
5 counts[ch]= counts.get(ch,0)+1
6
7print(counts)
8print(max(counts, key=counts.get))
main
word = "mammal" — six letters to count.
Memory
word= "mammal"
Console
output appears as the program runs…
Step 1/16
Algorithm in motion
Set intersection
Two grocery lists as sets — the intersection is one O(1) probe per item of the smaller scan.
slot 0slot 1slot 2slot 3
set Amilkeggsbreadjam
set Bbreadjamtearice
A & B————
1 / 10
Two grocery lists as sets. Intersection = items present in BOTH. Scan set A and probe set B for each item.
Challenge
Predict the output
Overwrites, dedupe and a safe lookup — trace each line.
predict.py
1d ={1:"a",2:"b"}
2d[2]="c"
3d[3]="d"
4s ={1,2,2,3,3}
5print(len(d))
6print(len(s))
7print(1in s)
8print(d.get(9,"none"))
Your predicted output — one line per console line
Hint: Assigning to an existing key overwrites, never duplicates. A set keeps one copy of each value. get() never raises.
Practice
Concept check
Click a term, then click its meaning to lock the pair.
8 conceptsTap a card to expand
?hash(key)drops into place
?key % bucketsdrops into place
?collisiondrops into place
?d.get(k, dflt)drops into place
?O(1) averagedrops into place
?setdrops into place
?[1, 2] as keydrops into place
?x in sdrops into place
Recap
Key takeaways
Key takeaways6 takeaways
01
dict and set give O(1) average lookups — the workhorse of Python problem solving.
02
key% buckets (or hash(key)) picks the slot; collisions chain inside the same bucket.
03
counts.get(ch, 0)+1 is the idiom for frequency counting without KeyError.
04
A seen-set or seen-dict finds duplicates, pairs and cycles in a single pass.
05
Keys must be hashable: strings, numbers, tuples — not lists or dicts.
06
Worst case, all keys collide into one bucket and lookups degrade to O(n).