collections.deque & High-Speed Ring Buffer Operations
1Concept
`collections.deque` is a doubly-linked ring buffer providing O(1) appends and pops from both ends (`appendleft`, `popleft`, `append`, `pop`). Setting `maxlen=N` creates a circular buffer that automatically evicts the oldest elements.
2Architecture Diagram
Deque (maxlen=3): [ e1, e2, e3 ] ---> append(e4) ---> [ e2, e3, e4 ] (e1 evicted automatically!)
3Code Example
Python 3.12
from collections import deque
# Circular memory buffer (last 3 events)
recent_logs = deque(maxlen=3)
for i in range(1, 6):
recent_logs.append(f"Event-{i}")
print(f"Added Event-{i} -> Buffer: {list(recent_logs)}")
# Fast O(1) FIFO Queue pop
oldest = recent_logs.popleft()
print(f"Popped from left: {oldest}")4Expected Output
Added Event-1 -> Buffer: ['Event-1'] Added Event-2 -> Buffer: ['Event-1', 'Event-2'] Added Event-3 -> Buffer: ['Event-1', 'Event-2', 'Event-3'] Added Event-4 -> Buffer: ['Event-2', 'Event-3', 'Event-4'] Added Event-5 -> Buffer: ['Event-3', 'Event-4', 'Event-5'] Popped from left: Event-3
5Key Takeaways
- ✓`deque.popleft()` is O(1); `list.pop(0)` is O(N) because it shifts all remaining elements in memory.
- ✓Circular buffers with `maxlen` are ideal for rate-limit sliding windows and real-time metric streams.
- ✓Deques support thread-safe atomic appends and pops from opposite ends.