The deque (or “deck”) is a rarely discussed collection in Python. While lists, dictionaries, and tuples dominate online tutorials, deques often go overlooked.
A deque (short for “double-ended queue”) is a unique and efficient data structure. Its key feature is that it only retains a fixed number of items—never exceeding a user-defined limit.
A double-ended queue holds up to the specified number of items, and never more.
It operates like a deck following the FIFO principle: First In, First Out. Once full, adding a new element automatically discards the oldest item from the left end.
Let’s explore some simple examples. First, import it: from collections import deque.
Creating a deque
Let’s build a deque with a maximum capacity of 3 elements.
# Create a deque limited to 3 items
my_deck = deque(maxlen=3)
# Add three elements
my_deck.append(1)
my_deck.append(2)
my_deck.append(3)
# Result
my_deck
# deque([1, 2, 3])Once the deque is full, adding another value pushes out the oldest item (1) from the left and inserts the new one ('extra') on the right.
# Adding beyond capacity removes the first element
my_deck.append('extra')
deque([2, 3, 'extra'])This behavior mirrors the FIFO system perfectly.
Adding Items to the Left
The name “double-ended queue” means you can add or remove elements from both ends. When appending to the left, the rightmost item gets dropped.
# Append 'left' to the front
my_deck.appendleft('left')
# [OUT]: deque(['left', 2, 3])
# Extend with more items to the front
my_deck.extendleft(['d', 'd'])
# [OUT]: deque(['d', 'd', 'left'])Rotating Elements
You can shift all elements one or more positions to the right or left using rotation.
# Create a fresh deque
my_deck = deque(maxlen=3)
my_deck.extend([1, 2, 3]) # deque([1, 2, 3])
# Rotate right by one
my_deck.rotate() # deque([3, 1, 2])
# Rotate left by one
my_deck.rotate(-1) # deque([1, 2, 3])You can also reverse the entire deque instantly.
my_deck.extend([1, 2, 3])
# Output: deque([1, 2, 3])
my_deck.reverse()
# Output: deque([3, 2, 1])Removing Elements
Items can be removed from the left, right, or by value.
my_deck = deque(maxlen=3)
my_deck.extend([1, 2, 3])
# Output: deque([1, 2, 3])
# Remove from the left
my_deck.popleft()
# Output: deque([2, 3])
# Remove from the right
my_deck.pop()
# Output: deque([1, 2])
# Remove a specific value
my_deck.remove('a')
# Output: deque([1, 3])To empty the deque completely, use clear().
my_deck.clear()
# Output: deque([])Practical Uses
1. Recent Search History (Memory Management)
Lists grow without bounds, but deque with maxlen is ideal for features like “Recently Viewed” or “Recent Searches,” automatically discarding older entries.
search_history = deque(maxlen=3)
search_history.append("Python tutorials")
search_history.append("Machine Learning")
search_history.append("Data Science")
search_history.append("Deep Learning") # "Python tutorials" is dropped
print(list(search_history))
# ['Machine Learning', 'Data Science', 'Deep Learning']2. Moving Average for Data Streams
For sensor data or stock prices, a deque provides an efficient sliding window to compute moving averages.
def moving_average(stream, window_size=5):
window = deque(maxlen=window_size)
for val in stream:
window.append(val)
if len(window) == window_size:
yield sum(window) / window_size
# Example: Temperature readings
data_stream = [20, 21, 20, 22, 23, 25, 24]
print(list(moving_average(data_stream, window_size=3)))3. Thread-Safe Task Queues
In CPython, .append() and .popleft() are thread-safe, making deque excellent for producer-consumer patterns where multiple threads share work.
import threading
from collections import deque
task_queue = deque()
def producer():
for i in range(5):
task_queue.append(f"Task {i}") # Safe across threads
def consumer():
while True:
try:
task = task_queue.popleft() # Also thread-safe
print(f"Processing {task}")
except IndexError:
breakHere’s how it works in practice:
producer()
task_queue
# Output: deque(['Task 0', 'Task 1', 'Task 2', 'Task 3', 'Task 4'])
consumer()
# Processing Task 0
# Processing Task 1
# Processing Task 2
# Processing Task 3
# Processing Task 4
task_queue
# Output: deque([])Final Thoughts
You’ve now learned a powerful alternative to standard collections. Use it to write cleaner, more efficient code.
Key takeaways:
- Define it with
deque(maxlen=n)to cap the number of items. - When full, new additions automatically remove the oldest item from the left.
- It supports any data type: integers, floats, strings, DataFrames, etc.
- Useful methods include
rotate,reverse,clear, andappendleft.
Found this helpful? Explore more of my work on my website.
References
collections – Container datatypes
Deque in Python – GeeksforGeeks



