List vs. Deque
Choosing the Right Structure
Learn when to use Python's list and when to reach for collections.deque based on the operations your program actually performs.
What Is a List?
A Python list is a dynamic array: elements are stored in a contiguous block of memory. This means every item sits right next to the previous one, like books lined up on a shelf.
What Is a Deque?
A deque (double-ended queue) from collections.deque is a doubly linked list of fixed-size blocks. Each node points to the next and previous block, enabling fast operations at both ends.
Why Does This Matter?
Choosing the wrong data structure can turn an \( O(1) \) operation into an \( O(n) \) one. When your program handles millions of elements, that difference is the gap between instant and frozen.
The Core Rule
Big-O at a Glance
| Operation | list | deque |
|---|---|---|
| Append (right) | \( O(1) \) amortized | \( O(1) \) |
| Pop (right) | \( O(1) \) | \( O(1) \) |
| Append (left) | \( O(n) \) insert(0, x) | \( O(1) \) appendleft |
| Pop (left) | \( O(n) \) pop(0) | \( O(1) \) popleft |
| Index access [i] | \( O(1) \) | \( O(n) \) |
| Slicing [i:j] | \( O(k) \) | Not supported |
The Key Distinction
How to Decide
Example 1: BFS Queue
Breadth-first search needs a FIFO queue: add to the right, remove from the left. A deque handles both in \( O(1) \).
Example 2: Sliding Window
A sliding window of size \( k \) over an array: we need to add to the right and remove from the left as the window advances. Classic deque territory.
Example 3: When list Wins
A scoring system where you need to look up any player's score by rank and occasionally sort: random access dominates.
maxlen: Deque's Superpower
Pass maxlen to create a bounded buffer. When new items arrive and the deque is full, old items are automatically evicted from the opposite end.
Visualizing the Cost Difference
When you call list.insert(0, x), every existing element must shift right by one position. With \( n \) elements, that is \( n \) copies.
Common Mistakes
Summary Comparison
| Feature | list | deque |
|---|---|---|
| Memory layout | Contiguous array | Linked blocks |
| Right append/pop | \( O(1) \) amortized | \( O(1) \) |
| Left append/pop | \( O(n) \) | \( O(1) \) |
| Random access [i] | \( O(1) \) | \( O(n) \) |
| Slicing | Supported | Not supported |
| maxlen auto-evict | No | Yes |
| Best for | Indexing, sorting | Queues, buffers |
Question 1 of 5
You are implementing a BFS traversal that repeatedly removes elements from the front of a queue. Which data structure should you use?
Question 2 of 5
What is the time complexity of accessing the element at index \( i \) in a deque of \( n \) elements?
Question 3 of 5
You need a rolling buffer that automatically discards the oldest entry when a new one is added and the buffer is full. Which approach is best?
Question 4 of 5
A program builds a leaderboard by sorting scores and then slicing the top 10. Which data structure is more appropriate?
Question 5 of 5
Consider this code that processes \( n \) items. What is its overall time complexity?
data = list(range(n))
while data:
item = data.pop(0) # remove from front
process(item) # O(1) work