Data Structures · Visualization
Deque: Pop Front
Deque
Popping from the front removes and returns the current first item, shrinking the deque from that end — the same as dequeueing a plain queue. Time: O(1). Space: O(1) per operation.
Big-O: Push/pop at both ends in O(1).
Deque: Pop Front
DequePopping from the front removes and returns the current first item, shrinking the deque from that end — the same as dequeueing a plain queue. The visualizer's default deque is empty, so popFront() on a fresh page reports 'Deque is empty — nothing to pop' instead of removing anything; after a pushBack(15) the front is 15 and popFront() removes it, leaving []. With the front index advanced modulo capacity, the operation is O(1) and never shifts the remaining items.
How Pop Front Works
- Check that the deque is not empty — an empty deque must report that there is nothing to pop.
- Read the value at the front, which is 15 after a pushBack(15) on the default empty deque.
- Advance the front index past that item, or unlink the front node.
- Return the removed value; if it was the last item, front and back both become empty.
Pop Front Worked Example
Input: popFront() on the default deque
- The default deque is empty, so the first frame says 'Deque is empty — nothing to pop'.
- After pushBack(15) the deque is [15]; popFront() now returns 15.
- The deque is empty again — front and back both point at nothing.
Output: Removed: 15, remaining deque: []
Press ▶ Run to watch it step by step.
from collections import deque
d = deque()
d.append(1) # push back
d.appendleft(2) # push front
d.pop() # pop back
d.popleft() # pop front
# O(1) at both ends
# example: pop front on [1, 2, 3]Behind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Pop Front Time & Space Complexity
When to Use Pop Front
- Serving the next task from the front of a work queue
- Removing outdated elements from the front of a sliding window
- Implementing 'redo' by taking the next state from the front
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.