Python Practice › Collections Programs › Program 12
Rotate a deque
Write a Python program to rotate a list of players for a round-robin game.
Output
1 ['A', 'B', 'C', 'D'] 2 ['B', 'C', 'D', 'A'] 3 ['C', 'D', 'A', 'B']
Approach
rotate(n)moves items from the end to the front.
Solution
from collections import deque players = deque(["A", "B", "C", "D"])for round_no in range(1, 4): print(round_no, list(players)) players.rotate(-1)Output
1 ['A', 'B', 'C', 'D'] 2 ['B', 'C', 'D', 'A'] 3 ['C', 'D', 'A', 'B']
Learn the concept
New to this topic? Read the lesson, then come back.