collections.deque · Python
pop / d[-1]
Removes and returns the rightmost element (stack pop); d[-1] peeks.
deque.pop(); d[-1]
Complexity
| Best case | O(1) |
|---|---|
| Worst case | O(1) |
| Extra space | O(1) |
Both ends are directly reachable.
Tested example
stack = deque()
stack.append("a")
stack.append("b")
assert stack.pop() == "b" # LIFO from the right end
assert stack[-1] == "a" # peek without removing
Behaviour
- Returns
- The rightmost element.
- Errors and preconditions
- IndexError if empty.
- Changes
- pop shrinks the deque by 1; d[-1] changes nothing.
- Memory and invalidation
- May free a block.