Python Practice › Iterator & Generator Programs › Program 15
Cycle through values
Write a Python program that assigns browsers to 7 tests in rotation.
Output
test 1 -> chromium test 2 -> firefox test 3 -> webkit test 4 -> chromium test 5 -> firefox test 6 -> webkit test 7 -> chromium
Approach
itertools.cyclerepeats a sequence forever.
Solution
from itertools import cycle browsers = cycle(["chromium", "firefox", "webkit"])for test in range(1, 8): print(f"test {test} -> {next(browsers)}")Output
test 1 -> chromium test 2 -> firefox test 3 -> webkit test 4 -> chromium test 5 -> firefox test 6 -> webkit test 7 -> chromium
Learn the concept
New to this topic? Read the lesson, then come back.