Python Practice › Collections Programs › Program 8
Queue with deque
Write a Python program that uses a deque as a first-in, first-out queue.
Output
serving Asha | waiting: ['Ravi', 'Meena'] serving Ravi | waiting: ['Meena'] serving Meena | waiting: []
Approach
appendto join,popleftto serve.
Solution
from collections import deque queue = deque()for customer in ["Asha", "Ravi", "Meena"]: queue.append(customer)while queue: print("serving", queue.popleft(), "| waiting:", list(queue))Output
serving Asha | waiting: ['Ravi', 'Meena'] serving Ravi | waiting: ['Meena'] serving Meena | waiting: []
Learn the concept
New to this topic? Read the lesson, then come back.