On this page
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
deque.append(self, item, /)— Add an element to the right side of the deque.deque.appendleft(self, item, /)— Add an element to the left side of the deque.deque.clear(self, /)— Remove all elements from the deque.deque.copy(self, /)— Return a shallow copy of a deque.deque.count(self, value, /)— Return number of occurrences of value.deque.extend(self, iterable, /)— Extend the right side of the deque with elements from the iterable.deque.extendleft(self, iterable, /)— Extend the left side of the deque with elements from the iterable.deque.index(...)— Return first index of value.deque.insert(self, index, value, /)— Insert value before index.deque.pop(self, /)— Remove and return the rightmost element.deque.popleft(self, /)— Remove and return the leftmost element.deque.remove(self, value, /)— Remove first occurrence of value.deque.reverse(self, /)— Reverse IN PLACE.deque.rotate(self, n=1, /)— Rotate the deque n steps to the right. If n is negative, rotates left.
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
- Fast ends, slow middle:
appendleft()andpopleft()are $O(1)$ (unlikelist.pop(0)which is $O(n)$), but random indexingq[mid]is $O(n)$. - No slicing:
q[1:3]raisesTypeError; useitertools.islice(q, 1, 3)instead.
REPL Inspect
from collections import deque
help(deque)
Linked from
- tuts/python3/stdlib_module/collections
- tuts/python3/stdlib_module/collections/ChainMap
- tuts/python3/stdlib_module/collections/Counter
- tuts/python3/stdlib_module/collections/defaultdict
- tuts/python3/stdlib_module/collections/namedtuple
- tuts/python3/stdlib_module/collections/OrderedDict
- tuts/python3/stdlib_module/collections/UserDict
- tuts/python3/stdlib_module/collections/UserList
- tuts/python3/stdlib_module/collections/UserString
Related notes
- tuts/python3/stdlib_module/collections/ChainMap Linked here; links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/Counter Linked here; links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/defaultdict Linked here; links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/namedtuple Linked here; links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/collections/OrderedDict links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/UserDict links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/UserList links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/UserString links to this page; shared tags: #collections, #stdlib
- tuts/rust/std/collections shared tags: #collections, #stdlib