Python deque index out of range: collections.deque Fix (2026)

You used collections.deque as a fast queue, called q.popleft(), and at some point got IndexError: pop from an empty deque. deque has the same empty-on-pop trap as list, with a few extra behaviors (maxlen, rotate, slicing limits) to know about.

Python deque index out of range collections.deque Fix (2026)
Python deque index out of range collections.deque Fix (2026)

📌 Quick answer: Guard popleft and pop with if my_deque:. Use maxlen on creation to cap size (older items auto-drop, no error). For random access, deque does NOT support slicing (use list(deque)[i:j] if needed).

Cause 1: popleft / pop on empty deque

Same empty-on-pop trap as list.pop, with a slightly different error message.

from collections import deque
q = deque([1, 2, 3])
while q:
    item = q.popleft()    # ✓ loop exits when q empty

# ❌ if you forgot the loop guard:
q.popleft()
q.popleft()
q.popleft()
q.popleft()    # IndexError: pop from an empty deque

Cause 2: Indexing past length

deque supports positional indexing but with the same out-of-range rules as list.

q = deque([1, 2, 3])
q[0]     # ✓ 1
q[2]     # ✓ 3
q[5]     # ❌ IndexError

Cause 3: Slicing not supported

deque does NOT support slice notation. Convert to list first.

q = deque([1, 2, 3, 4, 5])
q[1:3]    # ❌ TypeError: sequence index must be integer, not slice

# Workaround
sublist = list(q)[1:3]    # ✓ [2, 3]

Cause 4: maxlen for auto-cap (silent drop)

deque with maxlen never raises on push; older items silently drop off the opposite end.

recent = deque(maxlen=3)
recent.append(1); recent.append(2); recent.append(3); recent.append(4)
list(recent)    # ✓ [2, 3, 4] (1 was dropped, no error)

Prevention

  1. Use while q: as the consumer loop condition
  2. Add maxlen on creation when you only need the last N items
  3. For random access patterns, consider list (O(1) indexing) or deque + .rotate() carefully
  4. Catch IndexError if you need to detect drain-completion without an explicit empty check

Frequently Asked Questions

What does ‘pop from an empty deque’ mean?

You called deque.popleft() or deque.pop() on an empty deque. Same idea as list IndexError on empty list. Guard with ‘if my_deque:’ or catch IndexError.

Is collections.deque faster than list?

For FIFO queues: yes. deque.popleft() and appendleft() are O(1) vs list.pop(0) and insert(0, …) which are O(n). For LIFO stacks: both are O(1) at the right end.

Can I slice a deque?

Not directly with [start:end]. Convert to list first: list(my_deque)[1:3]. The conversion is O(n) so for large deques consider whether random-access slicing is the right pattern.

What does deque(maxlen=N) do?

A bounded deque. When you push to the full deque, the oldest element on the opposite end silently drops. Common for rolling windows: store the last N samples without manual trimming.

How is deque.rotate() useful?

Cyclic shift: deque.rotate(1) moves every element right by 1 (last wraps to front). deque.rotate(-1) shifts left. O(k) where k is the rotation count. Useful for circular buffers or implementing carousels.

Leave a Comment