Rotate a deque

EasyCollections Programs · 12 of 20

Write a Python program to rotate a list of players for a round-robin game.

Advertisement
Output
1 ['A', 'B', 'C', 'D']
2 ['B', 'C', 'D', 'A']
3 ['C', 'D', 'A', 'B']

Approach

  1. rotate(n) moves items from the end to the front.

Solution

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

More collections programs

← All 20 Collections Programs

Leave a Comment