CS concepts

Python Big-O Cheat Sheet Explained: The Costs That Cause Timeouts

Updated Nov 27, 2026 · 3 min read

In Python, x in list, list.pop(0) and adding to a string in a loop are O(n) each, so inside a loop they turn into O(n²). Dict and set lookups are O(1) on average, deque.popleft() is O(1), and heapq push and pop are O(log n). Most interview timeouts are fixed by swapping one of those slow lines for its fast twin.

Watch onYouTubeInstagram

Key points

  • ●x in list is O(n); x in set is O(1) on average. Convert once, then look up.
  • ●list.pop(0) shifts every item; use collections.deque and popleft() for a queue.
  • ●Build strings by collecting pieces in a list and calling ''.join once.
  • ●Sort once outside the loop, or keep a heap with heapq at O(log n) per push or pop.
  • ●Slicing a[i:j] copies k items, so it costs O(k).
Step-by-step visual sheet: Python Big-O Cheat Sheet
The full visual sheet, made with Edithly.Open the interactive sheet

Right logic, still too slow

Your solution is correct. It passes the small tests. Then it times out at 100,000 items.

The usual cause is one line of Python that looks cheap and isn't. x in my_list inside a loop is O(n²). At 100,000 items that's 10 billion steps. The fix is often a single line, but you have to know which operations cost what.

The costs worth memorizing

These are CPython costs, average case where stated, and they're the core of the visual sheet above.

Structure Operation Cost
list append(x), pop() from the end O(1) (append is amortized)
list len(a), a[i] O(1)
list pop(0), insert(0, x) O(n), every element shifts
list x in a, a.index(x), min, max, sum O(n)
list a[i:j] O(k), copies k items
list sort(), sorted() O(n log n)
dict / set d[key], key in d, x in s, add, remove O(1) average
dict / set set(a), Counter(a) O(n) to build
deque append, appendleft, pop, popleft O(1)
heapq heappush, heappop O(log n)
heapq heapify(a) O(n)
heapq heap[0] O(1)
bisect bisect_left on a sorted list O(log n)
bisect insort O(n), insertion shifts
str s + t O(len(s) + len(t))

Four swaps that fix most timeouts

1. Membership in a loop: list to set.

# O(n^2): each "in" scans the list
common = [x for x in a if x in b]

# O(n): build the set once, then O(1) lookups
b_set = set(b)
common = [x for x in a if x in b_set]

2. A queue: list.pop(0) to deque.popleft().

from collections import deque

queue = deque([start])
while queue:
    node = queue.popleft()  # O(1), not O(n)

This matters most in BFS, where the queue is used on every step. A list works on small inputs and then falls over on big ones, which is exactly the kind of bug that hides until the hidden tests run.

3. Building a string: += to join.

parts = []
for word in words:
    parts.append(word)
result = "".join(parts)  # O(n) total

Adding to a string in a loop builds a new string every time, which is O(n²) in total.

4. Sorting in a loop: sort once, or use a heap.

Sorting inside a loop of n steps is O(n² log n). Sort once before the loop. If items keep arriving and you need the smallest each time, keep a heap: heappush and heappop are O(log n) each.

Quick recall

The bottom of the sheet groups everything by cost:

  • O(1): len(a), a[i], popping the end of a list, deque operations, heap[0], dict and set lookups on average
  • O(log n): heappush, heappop, bisect_left
  • O(n): list scans, shifting the front of a list, heapify, building a set or Counter, insort, list-plus-join string building
  • O(k): slicing a[i:j]
  • O(n log n): sort(), sorted()
  • O(n²): list membership or string addition inside a loop
  • O(n² log n): sorting inside a loop

The trap

Slicing looks free and isn't. a[i:j] copies every item in the slice. Writing a[1:] inside a recursive function copies almost the whole list on every call. Pass indexes instead of slices when the list is large.

Dict and set lookups being O(1) on average is why so many fixes end with one: whenever you catch yourself searching a list over and over, build a set or a dict first.

Frequently asked questions

What is the time complexity of x in list in Python?

O(n), because Python scans the list item by item. Inside a loop over n items that's O(n²). Turn the list into a set first and each check is O(1) on average.

Why is list.pop(0) slow in Python?

Removing the first item shifts every other item one place left, so it's O(n). Popping from the end with pop() is O(1). For a queue, such as in BFS, use collections.deque and popleft(), which is O(1).

What is the time complexity of Python's heapq?

heappush and heappop are O(log n), heapify on a list is O(n), and peeking at heap[0] is O(1).

Is string concatenation in a loop O(n²) in Python?

Treat it that way. s + t builds a new string, so adding to a string in a loop copies it each time and costs O(n²) in total. Collect the pieces in a list and call ''.join once for O(n).

Turn any problem or document into a visual sheet like this one

Paste a LeetCode problem, a PDF or a link into Edithly and get a step-by-step sheet you can study, share or quiz yourself on.

Make this sheet in Edithly, free