← Data Structures
2 / 25
Lesson 2 — Queue
Queue: first in, first out
A queue is like a line at a shop: people join at the back and are served from the front — FIFO (first in, first out). Removing from the front of a normal list is slow on big lists, so Python offers deque ("deck", a double-ended queue) in the collections module. append() adds at the back, popleft() removes from the front — both instantly.
from collections import deque
q = deque(["Ann", "Bob"])
q.popleft() # "Ann"
Why it matters: printers, web servers and message systems all process jobs in arrival order with queues.
Want it another way?
Task
Add "Cid" to the back of the queue, then serve (popleft) two customers. Expected: Ann, Bob, then ['Cid'] still waiting.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
Test your knowledge
A quick 3-question quiz on this lesson — instant feedback.