Python's Itertools.cycle()


Python tip:

Do you need to repeat a sequence over and over again?

Use cycle from itertools 👇

from itertools import cycle

chord_sequence = cycle(["G", "D", "e", "C"])

song_chords = [next(chord_sequence) for _ in range(16)]

print(song_chords)
# ['G', 'D', 'e', 'C', 'G', 'D', 'e', 'C', 'G', 'D', 'e', 'C', 'G', 'D', 'e', 'C']