Queue Data Structure in Python with deque
First in, first out: serve items in arrival order with collections.deque, fast at both ends.
- append() and popleft() on a deque
- Why list.pop(0) is slow
- Where queues are used
Join at the back, leave from the front
The problem
Print jobs, customer support tickets and messages between programs must be handled in the order they arrive. A queue keeps that order, and choosing the right tool for it in Python keeps it fast even with millions of items.
Think of it like this
A queue is like a line at a ticket counter. New people join at the back, and the person at the front is served first. Nobody skips ahead, so everyone is served in the order they arrived.
deque(["first", "second"])🚶🚶 Two people already waitingqueue.append("third")🚶 A new person joins at the backqueue.popleft()🎫 The person at the front is servedlist(queue)👀 Who is still waitingA queue is a first-in, first-out (FIFO) collection: items are added at the back and removed from the front.
Key points
deque(["first", "second"])creates a queue that already holds two items.queue.append("third")adds an item at the back.queue.popleft()removes and returns the item at the front, here"first".dequeremoves from the front quickly; a list would have to shift every remaining item.
When should you use it?
- Processing tasks or requests in arrival order.
- Breadth-first search in graphs and grids.
- Buffers between producers and consumers, such as messages or events.
When not to use it
- The most recent item should come out first: use a stack.
- Items have priorities: use
heapqfor a priority queue. - Several threads share the queue:
queue.Queuehandles locking for you.
See how it works
Enqueue and dequeue to see that items leave in the same order they arrived.
Example code
from collections import deque
queue = deque(["first", "second"])
queue.append("third")
print("served:", queue.popleft())
print("waiting:", list(queue))
Result of running the code
served: first
waiting: ['second', 'third']
Code explanation
deque comes from the collections module and is built for adding and removing at both ends. append adds third at the back, and popleft removes the item at the front, which is first, because it arrived earliest. Converting to a list at the end shows the items still waiting, in their order of arrival.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Try empty, single-item, and duplicate-value inputs.
- Print state after every step to observe the algorithm.
- Benchmark 100, 1,000, and 10,000 items, then compare another approach.
Common mistakes
Using a list and pop(0)
pop(0) shifts every remaining item, which gets slow for long queues.
queue = ["first", "second"]
queue.pop(0)
queue = deque(["first", "second"])
queue.popleft()
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Course Schedule Order Order courses so every prerequisite comes first. Hard
- Can We Split Into Two Teams? Decide whether people can be assigned to two teams so that each listed pair is on opposite teams. Medium