Cycle Through Teams
Intermediate · Write the code · from Python Iterators: How for Loops Really Work Under the Hood
From the lesson: What Can You Do with itertools?
Python's itertools module is a toolbox of powerful iterator utilities. Instead of building everything from scratch, you can combine these building blocks.
Read this part of the lessonUse itertools.cycle to assign 6 players to teams in round-robin order. Given teams = ['Alpha', 'Beta', 'Gamma'] and players = ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve', 'Frank'], print each assignment as Player -> Team.
import itertools
teams = ['Alpha', 'Beta', 'Gamma']
players = ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve', 'Frank']
# Use itertools.cycle to cycle through teams
# Print: Alice -> Alpha, Bob -> Beta, etc.
More Iterator exercises
- Manual Iteration with next()Intermediate
- Build a Range-Like IteratorAdvanced
- Fix the Broken IteratorIntermediate
- Build a Fibonacci IteratorAdvanced
- Chain and Filter with itertoolsAdvanced
All Python exercises · 50 practice problems · Advanced Python quiz