Python deque pop() and popleft(): Syntax and behavior
Learn how deque.pop() and deque.popleft() work in Python, including return values, empty-deque errors, performance tradeoffs, and when to choose deque over list.

When you work with a Python collections.deque, removing elements from either end is a core operation. deque.pop() removes and returns the rightmost element, and deque.popleft() removes and returns the leftmost element. Both methods take no arguments. They are what make a deque useful as a queue or a stack replacement in performance-sensitive code.
How pop() and popleft() Remove Elements
deque.pop() removes and returns the rightmost element. deque.popleft() removes and returns the leftmost element. Both run in O(1) time because the deque is implemented as a doubly-linked list of fixed-size blocks.
from collections import deque
d = deque([1, 2, 3, 4])
right = d.pop() # right = 4, d is now deque([1, 2, 3])
left = d.popleft() # left = 1, d is now deque([2, 3])The return value is the removed element itself. If you only need to discard the element, you can ignore the return value; the method still performs the removal.
What Happens When the Deque Is Empty
Both methods raise IndexError when called on an empty deque. This is the same exception type that list.pop() raises, but the message differs slightly.
from collections import deque
empty = deque()
try:
empty.popleft()
except IndexError as e:
print(e) # pop from an empty dequeIn a producer-consumer pattern where multiple threads drain the same deque, you cannot assume an element is available just because the deque was non-empty a moment earlier. Guard the call with a check or catch the exception, depending on whether the race is expected.
Why deque Is Faster Than list for Left Removal
A list stores elements in contiguous memory. Removing the first element with list.pop(0) shifts every remaining element one position to the left, an O(n) operation. A deque stores elements in fixed-size blocks linked together, so removing from either end only adjusts pointers and block bookkeeping. That is the core reason to choose deque.popleft() over list.pop(0) when the left end is removed frequently.
For small collections the difference is negligible, but for a queue that processes thousands of items per second, the shift cost of a list becomes visible. The deque avoids that cost entirely.
Practical Usage: Queue and Sliding Window
The most common pattern is a FIFO queue where items are appended on the right and consumed from the left.
from collections import deque
queue = deque()
queue.append('task-1')
queue.append('task-2')
queue.append('task-3')
while queue:
task = queue.popleft()
# process taskFor a sliding window over a stream, a deque with maxlen keeps the window bounded by discarding the opposite end automatically when a new item is appended at capacity.
from collections import deque
def sliding_window(stream, size):
window = deque(maxlen=size)
for item in stream:
window.append(item)
# When maxlen is reached, the leftmost item is discarded automatically.
yield list(window)Because the deque discards old items automatically at capacity, you do not need to call popleft() manually in that scenario. Calling it explicitly still works when the deque is not at capacity.
Edge Cases and Common Mistakes
A frequent mistake is assuming popleft() exists on a regular list. It does not; list only has pop() and pop(index). Another mistake is using deque.pop() when the intent was to remove from the left, which silently changes the order of processing in a queue.
Another edge case involves maxlen. When the deque is at capacity, appending a new item automatically discards an item from the opposite end. If you maintain a separate count of items in the deque, that count can drift out of sync because items disappear without an explicit popleft() call.
Performance Considerations
Both pop() and popleft() are O(1). There is no reallocation or shifting of elements. The memory overhead per element is higher than a list because a deque uses linked blocks with extra bookkeeping, but for large collections the difference is usually acceptable when the access pattern is append and remove from the ends.
If you need random access by index in the middle of the collection, a deque is a poor choice. Indexing into a deque is O(n) in the worst case because it must traverse blocks. In that scenario, a list or another structure is more appropriate.
When to Choose deque Over list
Use deque when you append and remove from both ends, when you need a FIFO queue with O(1) left removal, or when you process a stream with a bounded window.
Use list when you need fast random access by index, when the collection is small and the left-removal cost is negligible, or when you need slicing and other list-specific operations.
For a one-off script processing a few hundred items, the difference rarely matters. For a long-running service that drains a queue continuously, deque.popleft() is the correct choice because the cost of removing from the left does not grow with the number of elements.
Try matching browser tools
All toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.