Python's Built-in Operations: A Big-O Cheat Sheet

Time complexity of operations on Python's built-in types

Python's Built-in Operations: A Big-O Cheat Sheet

The official Python documentation details the time complexity of operations on built-in types, from lists and dicts to memoryviews and ranges. It covers average-case costs, worst-case scenarios for hash collisions, and notes on implementation specifics like CPython's list resizing and frozendict's immutability.

If you need to add or remove at both ends, consider using a collections.deque instead.
  1. alexpotato

    Dave Beazley has a great talk about using Python built ins [0] for data analysis and other quick operations.

    As a meta note, I've used many of these builtins over the years but, due to LLMs, have been using them less and less. Re-watching the video almost felt like watching bushcrafters make a chair using just a knife and saw...

    0 - https://www.youtube.com/watch?v=lyDLAutA88s

  2. Retr0id

    Excellent. Previously this was only documented semi-unofficially on the wiki here: https://wiki.python.org/moin/TimeComplexity

  3. StellarScience

    Python is famously built around hash tables. So much so that several versions ago they made an improvement to the hash table implementation, and the entire language became several percent faster.

    However, I'm surprised to see no data structures at all with O(log(N)) complexity. Surely there are some use cases for which that's desirable?

  4. mwkaufma

    Isn't O(n - k) or O(len(l1) + len(l2)) just O(n)? Instead of blurring the line between complexity-analysis and cycle-counting, just print both the complexity and the est proportional cycle-count as separate measures.

  5. wodenokoto

    Why are `min(r)` and `max(r)` for range objects o(n) ?

    I thought min and max where constants stored in the object. Basically you are just asking for one of the parameters it was created with.

More from this day

2026-08-29