collections.deque · Python
popleft
Removes and returns the leftmost element: the dequeue operation of a FIFO queue.
deque.popleft()
Complexity
| Best case | O(1) |
|---|---|
| Worst case | O(1) |
| Extra space | O(1) |
Advance the left index; free the block when it empties.
Tested example
queue = deque([1, 2, 3])
assert queue.popleft() == 1 # FIFO: first in, first out
assert queue.popleft() == 2
queue.popleft()
try:
queue.popleft()
raise AssertionError("expected IndexError")
except IndexError:
pass
Behaviour
- Returns
- The leftmost element.
- Errors and preconditions
- IndexError if the deque is empty.
- Changes
- Length shrinks by 1.
- Memory and invalidation
- May free a block.