deque is for O(1) operations at both ends; list is for random access
collections.deque is a doubly linked list of fixed-size blocks. It provides O(1) append and pop from both ends, and it supports an optional maxlen for bounded buffers. A list is better for O(1) random access and memory locality, but its front operations are O(n) because elements shift. Use deque for queues, BFS frontiers, sliding windows, and ring buffers. Use list when you mostly index, iterate, and append/pop at the end.
deque.popleft() and deque.appendleft() are O(1); list.pop(0) and list.insert(0, x) are O(n).
deque indexing in the middle is O(n) in CPython, so do not use it as a random-access array.
maxlen gives automatic eviction of the oldest item, which is useful for bounded caches and windows.
Common mistake: assuming deque is always faster. For iteration and indexing, list is often faster due to contiguous memory.
Version note: deque has been available since Python 2.4; maxlen is stable and widely supported.
0-2 years experience
2-5 years experience
5-8 years experience
8+ years experience