Codectionary / Developer documentation / Python

deque: queues and bounded history

Use efficient operations at both ends of a collection.

How it works

A deque supports appending and removing from either end. It is a useful standard-library choice for a first-in, first-out queue where repeatedly removing list index zero would shift remaining elements.

An optional maxlen creates a bounded deque. Appending to a full bounded deque automatically discards an item from the opposite end. That behaviour suits history buffers, not queues where every item must be processed.

Choices and common mistakes

popleft and pop raise IndexError on an empty deque. Check the workflow or handle that condition.

A deque is not a drop-in replacement for a list in every algorithm. Middle indexing is slower than indexing near its ends, and slicing is not supported directly.

Try it yourself

Maintain the last three visited page names with maxlen=3. Add four names and explain which one is removed.

Syntax

from collections import deque
queue = deque()
queue.append(item)
item = queue.popleft()

Examples

Process arrivals in order

Build a small FIFO work queue.

from collections import deque
queue = deque(["first", "second"])
queue.append("third")
while queue:
    print(queue.popleft())
  1. append places new work on the right. popleft removes the oldest remaining item from the left.
  2. The while condition stops before another removal from an empty queue, avoiding IndexError.
  3. This demonstrates queue ordering; asynchronous or multi-threaded workers may need a dedicated queue abstraction.
  4. Expected output: first second third

Keep a short history

Retain only the newest three observations.

from collections import deque
history = deque(maxlen=3)
for value in [10, 20, 30, 40]:
    history.append(value)
print(list(history))
  1. The first three values fit. Appending forty to the full deque discards ten from the opposite end.
  2. list shows the remaining values in order. The limit bounds the number of stored items, not their individual memory sizes.
  3. Use bounded history for recent status messages, not for data that must never be dropped.
  4. Expected output: [20, 30, 40]

Rotate the next item

Move a cyclic schedule without rebuilding it.

from collections import deque
turns = deque(["John", "James", "Sam"])
turns.rotate(-1)
print(list(turns))
turns.rotate(1)
print(list(turns))
  1. A negative rotation moves items left, placing the former first item at the right. A positive rotation reverses that direction.
  2. Rotation changes the deque in place. No participant is removed from the schedule.
  3. Use a small rotating queue for round-robin selection when equal turns are appropriate.
  4. Expected output: ['James', 'Sam', 'John'] ['John', 'James', 'Sam']

Best practices

  • popleft and pop raise IndexError on an empty deque. Check the workflow or handle that condition.
  • A deque is not a drop-in replacement for a list in every algorithm. Middle indexing is slower than indexing near its ends, and slicing is not supported directly.

Runtime & versions

Python interpreter. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Python language reference

Specifications & further reading