Python dicts and sets can hit quadratic time: the O(1) assumption breaks down

lemire · x · 2026-09-04

Daniel Lemire publishes an article examining the widely believed claim that Python's dict and set are strictly O(1) for insertions and lookups — and shows why it doesn't hold.

Starting from hash functions (random-like mappings from objects to integers) and bucket arrays, he explains how adversarial or degenerate inputs cause collisions to pile up, degrading operations to quadratic time. The takeaway: O(1) is an average-case expectation, not a hard guarantee, and understanding hashing behavior is key to avoiding the trap.

Original post →

More from coding & agent

coding & agent channel →