collections.deque · Python

pop / d[-1]

Removes and returns the rightmost element (stack pop); d[-1] peeks.

deque.pop(); d[-1]

Complexity

Best caseO(1)
Worst caseO(1)
Extra spaceO(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
Runs in the test suite on CPython 3.14.0 · examples/python/tests/test_collections_deque.py

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.