Visualizing Python Dict Hashing, Collisions, and ResizingVisualizing Python Dict Hashing, Collisions, and Resizing
The network for creativity
Join 1.25M professional creatives like you
Connect with clients, get discovered, and run your business 100% commission-free
Creatives on Contra have earned over $150M and we are just getting started
Drawing an algorithm as an object: why a Python dict never fills up.
I wanted to show the moment a hash table runs out of room, so each slot became a physical tile. A key drops in, its hash picks a slot, and a collision slides it to the next one.
The rules come straight from CPython's dictobject.c. A dict starts with 8 slots, only two thirds are usable, and when it's full it grows to used × 3 rounded up to a power of two, so 8 becomes 16. The stored hashes are reused and every key is placed again.
The probing shown is simplified (plain "try the next slot"), and I say so on screen. CPython's real probe, 5i + 1 + perturb, is the next piece.
Drawn and animated in code, frame by frame, with a blind read-through before release to check that it explains without narration.
If your product has a mechanism people don't get on first look, I make explainers like this one. The service is on my profile.
Eros's avatar
Saying on screen that the probing is simplified is a good choice: the video stays easy and still honest. I didn't know that only two thirds of the slots are usable. Will the next piece show how perturb spreads the keys, next to the plain "try the next slot" rule?
Arts's avatar
DevLMS logo
Thanks Eros! Yes, that's the plan: the same 8 tiles side by side, plain "next slot" on one side and CPython's probe on the other. Perturb is the fun part. It starts as the full hash and shifts right by 5 bits each step, so the high bits of the hash get a say and colliding keys...
Eros's avatar
Sounds great! I like that the high bits of the hash get a say, so the keys don't pile up. Looking forward to the side-by-side version.
Back to feed
The network for creativity
Join 1.25M professional creatives like you
Connect with clients, get discovered, and run your business 100% commission-free
Creatives on Contra have earned over $150M and we are just getting started