Cycle through values

EasyIterator & Generator Programs · 15 of 20

Write a Python program that assigns browsers to 7 tests in rotation.

Advertisement
Output
test 1 -> chromium
test 2 -> firefox
test 3 -> webkit
test 4 -> chromium
test 5 -> firefox
test 6 -> webkit
test 7 -> chromium

Approach

  1. itertools.cycle repeats a sequence forever.

Solution

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

More iterator & generator programs

← All 20 Iterator & Generator Programs

Leave a Comment