Python dict/set operations can degrade to O(n) time under crafted hash collisions
A technical analysis by Valentin Ignatev, discussed by a Python performance blogger, shows that Python's set and dict data structures, though usually treated as constant-time O(1) for lookups and insertions, can be forced into quadratic-time behavior. By constructing integers that share the same hash value (using a Mersenne prime modulus), it's possible to trigger repeated hash collisions that slow insertion and lookup dramatically as the collection grows.
GoKawiil's interpretation of the reporting above, not reported fact.
Many developers assume dictionaries and sets in Python guarantee near-instant performance regardless of scale, but this assumption can break down under adversarial or specially structured input, creating a potential denial-of-service vector in real-world applications. It's a reminder that hash-based data structures have worst-case behavior that engineers building performance-critical or security-sensitive systems need to account for.
- Python's set and dict are hash tables, not truly O(1) in worst-case scenarios.
- Carefully chosen integers can be made to collide, forcing quadratic-time insertions and lookups.
- This has implications for security (denial-of-service) and performance-critical Python code.
Fluent Python by Luciano Ramalho — If this dive into how dict and set hashing can degrade into quadratic behavior hooked you, Fluent Python digs deep into Python's data model, hashable objects, and the internals behind dicts and sets. It's the book that turns "I use dictionaries" into "I know why this dictionary is slow.
See Fluent Python by Luciano Ramalho on Amazon → Affiliate link — we may earn a commission on purchases, at no extra cost to you. Product picked by AI based on this article; it is not a tested recommendation.Source: lemire.me — Https, 2026-09-08
Published there as: “Python sets and dictionaries can have quadratic-time performance”
Read the original report → The summary and analysis above are GoKawiil's own, written from reporting by the source above. Facts and quotes belong to the original publisher.