collections.deque · Python

appendleft

Adds an element at the left end.

deque.appendleft(x, /)

Complexity

Best caseO(1)
Worst caseO(1)
Extra spaceO(1)

Symmetric to append; compare list.insert(0, x), which is O(n).

Tested example

items = deque([2, 3])
items.appendleft(1)  # O(1); list.insert(0, x) would be O(n)
assert list(items) == [1, 2, 3]
Runs in the test suite on CPython 3.14.0 · examples/python/tests/test_collections_deque.py

Parameters

x
The element.

Behaviour

Returns
None.
Errors and preconditions
None.
Changes
Length grows by 1; with a full maxlen, the rightmost element is discarded.
Memory and invalidation
Allocates a block when the leftmost one is full.