deque(maxlen=n)
Standard Library Idioms

Meaning

Creates a bounded deque with a maximum length n; when the deque is full, adding new elements automatically discards the oldest elements.

Primary Function

Creates a fixed‑size double‑ended queue that automatically evicts the oldest items when the size limit is exceeded.

Communicative Purpose

Expresses the intent to maintain a fixed‑size rolling buffer for efficient FIFO/LIFO operations with automatic eviction.

Pattern

deque(maxlen=<n>)

Core Structure

deque(maxlen=n)

Função primária

Creates a fixed‑size double‑ended queue that automatically evicts the oldest items when the size limit is exceeded.

Propósito comunicativo

Expresses the intent to maintain a fixed‑size rolling buffer for efficient FIFO/LIFO operations with automatic eviction.

Situações de gatilho

When you need a sliding window of recent items, a bounded history buffer (e.g., recent actions, sensor readings), or a fixed‑size queue/stack where old entries should be discarded automatically.

Contextos

Used in streaming data processing, sliding‑window algorithms, limited‑size undo/redo stacks, breadth‑first search with depth limits, caching recent requests, and any scenario requiring a fixed‑size deque.

Padrão

deque(maxlen=<n>)

Estrutura central

deque(maxlen=n)

Slots de substituição

n: integer specifying the maximum number of items the deque can hold.

Colocados típicos

  • append
  • pop
  • popleft
  • extend
  • rotate
  • popleft
  • appendleft

Substituições comuns

  • deque() (unbounded deque)
  • list with slicing for a sliding window
  • collections.deque without maxlen.

Erros comuns

Assuming maxlen limits both ends equally (it discards from the opposite end of insertion); confusing maxlen with the maximum size of a list; forgetting that appending to the left discards from the right and vice‑versa.

Similar / contraste

list slicing for sliding windows, collections.deque without maxlen for an unbounded deque, array module for fixed‑size C‑style arrays, queue.Queue with maxsize for thread‑safe bounded queues.

Interferências

Do not confuse deque.maxlen with the maxsize parameter of queue.Queue or with list length limits; remember that the discard occurs on the opposite side of the insertion side.

Família do chunk

  • deque
  • deque(maxlen=n)
  • deque.rotate

Nuance

When appending to the right, the oldest items are removed from the left; when appending to the left, the oldest items are removed from the right, maintaining a strict rolling window.

Efeito pragmático

Communicates the intention to use an efficient, fixed‑size double‑ended queue that automatically manages its size, signaling a performance‑conscious, bounded‑buffer pattern.

Dica de memória

Think of a rolling window of fixed size n that always shows the most recent n items.

Upgrade path

Using collections.deque without maxlen for an unbounded deque, or using itertools.islice on an iterable for a sliding window.

Tag de espaçamento: Medium-termIdioma?: Sim

Log in to save chunks.