collections.deque · Python
appendleft
Adds an element at the left end.
deque.appendleft(x, /)
Complexity
| Best case | O(1) |
|---|---|
| Worst case | O(1) |
| Extra space | O(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]
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.