Cycle over list indefinitely

You can use itertools.cycle, to cycle around the values in a, b and c as specified:

from itertools import cycle
for i in cycle([a,b,c]):
    print(f'x: {i}')

Output

x: 1
x: 2
x: 0
x: 1
x: 2
x: 0
x: 1
x: 2
x: 0
x: 1
...

You could use cycle() and call next() as many times as you want to get cycled values.

from itertools import cycle

values = [1, 2, 3]
c = cycle(values)

for _ in range(10):
    print(next(c))

Output:

1
2
3
1
2
3
1
2
3
1

or as @chepner suggested without using next():

from itertools import islice

for i in islice(c, 10):
    print(i)

To get the same result.


this is what itertools.cycle does

import itertools,time

for i in itertools.cycle([1,2,3]):
    print(i)
    time.sleep(1)