Skip to content

deque

A deque, short for double-ended queue and pronounced “deck,” is a sequential collection that supports inserting and removing elements at both of its ends in constant time. It generalizes the stack and the queue, which tie each operation to a fixed end.

Operations are named for the side they act on, such as push-front and push-back for insertion or pop-front and pop-back for removal. Access stays at the two ends, so a deque cannot reach into its middle efficiently.

Push and pop at either end below to watch the sequence grow and shrink from that side:

Interactive diagram — enable JavaScript to view.

Two implementations are common:

  • Doubly linked list: links each element to its neighbors, giving constant-time updates at both ends but no constant-time indexing.
  • Growable ring buffer: stores elements in a circular array, giving amortized constant-time work at the ends plus constant-time indexing.

Deques back sliding-window algorithms and work-stealing schedulers, and a bounded deque keeps only the most recent items by discarding from the far end as new ones arrive. Python provides collections.deque, with O(1) appends and pops at either side, where a list pays an O(n) cost for the same work at its front.

Python's deque: Implement Efficient Queues and Stacks

Tutorial

Python's deque: Implement Efficient Queues and Stacks

Use a Python deque to efficiently append and pop elements from both ends of a sequence, build queues and stacks, and set maxlen for history buffers.

intermediate data-structures python stdlib

For additional information on related topics, take a look at the following resources:


By Martin Breuss • Updated July 18, 2026