On this page
  1. collections.deque
  2. Import
  3. Signature & Overview
  4. Methods & Attributes
  5. Usage
  6. Gotchas
  7. REPL Inspect

collections.deque

Prev: collections.defaultdict | Up: collections | Next: collections.namedtuple
Rel: collections.ChainMap, collections.Counter, collections.defaultdict, collections.namedtuple
Ref: docs.python.org — collections.deque
Tags: #public #python3 #stdlib #collections


Import

import collections
from collections import deque

Signature & Overview

collections.deque(...)

A list-like sequence optimized for data accesses near its endpoints.

Methods & Attributes

Usage

from collections import deque

# O(1) appends/pops from both ends; maxlen creates a fixed-size sliding window
q = deque([1, 2, 3], maxlen=3)
q.append(4)          # drops 1 from the left automatically
assert list(q) == [2, 3, 4]
assert q.popleft() == 2

Gotchas

REPL Inspect

from collections import deque
help(deque)

Linked from

Related notes