Python Algorithms

Queue Data Structure in Python with deque

First in, first out: serve items in arrival order with collections.deque, fast at both ends.

  • Intermediate
  • 2 min read
  • 1 runnable example
What you'll learn
  • append() and popleft() on a deque
  • Why list.pop(0) is slow
  • Where queues are used
deque(["first", "second", "third"])
firstfront
second
thirdback
↑ popleft() serves "first"

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.

From a ticket line to code
deque(["first", "second"])🚶🚶 Two people already waiting
queue.append("third")🚶 A new person joins at the back
queue.popleft()🎫 The person at the front is served
list(queue)👀 Who is still waiting
First in, first out: the order of arrival is the order of service.
In one sentence

A queue is a first-in, first-out (FIFO) collection: items are added at the back and removed from the front.

Key points

  1. deque(["first", "second"]) creates a queue that already holds two items.
  2. queue.append("third") adds an item at the back.
  3. queue.popleft() removes and returns the item at the front, here "first".
  4. deque removes 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 heapq for a priority queue.
  • Several threads share the queue: queue.Queue handles locking for you.

See how it works

Enqueue and dequeue to see that items leave in the same order they arrived.

Example code

Run code →
main.py
from collections import deque

queue = deque(["first", "second"])
queue.append("third")

print("served:", queue.popleft())
print("waiting:", list(queue))
Get a Run button for this example

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.

  1. Try empty, single-item, and duplicate-value inputs.
  2. Print state after every step to observe the algorithm.
  3. Benchmark 100, 1,000, and 10,000 items, then compare another approach.
Run in the Python compiler →

Common mistakes

Using a list and pop(0)

pop(0) shifts every remaining item, which gets slow for long queues.

Problem
queue = ["first", "second"]
queue.pop(0)
Fix
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.

All challenges