Paul's Python CoursePPC

Queues

Not met yet

A queue is used from both ends: things join at the back and leave from the front, so the first in is the first out, like people at the coffee machine. A list can do it with append and pop(0), but pop(0) shuffles everything behind it up a place, so serving each one is O(n). Use collections.deque, where joining and leaving at either end are O(1).

Try it

main.py

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks a queue serves the most recent arrival first, as a stack would.
  • Thinks taking the first item off a list with pop(0) is as quick as taking the last with pop().

Where it comes up