Queue with deque

EasyCollections Programs · 8 of 20

Write a Python program that uses a deque as a first-in, first-out queue.

Advertisement
Output
serving Asha | waiting: ['Ravi', 'Meena']
serving Ravi | waiting: ['Meena']
serving Meena | waiting: []

Approach

  1. append to join, popleft to serve.

Solution

Python
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.

More collections programs

← All 20 Collections Programs

Leave a Comment