Say you have this:
foo = [1,2,3,4,5,6,7,8,9,10]
bar = 22
I want to get bar
many values from foo
, repeating from the start after reaching the end. So for these inputs, the results should be 1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2
.
I have this working code:
foo = [1,2,3,4,5,6,7,8,9,10]
bar = 22
x = 0
for i in range(bar):
print(foo[x])
# increment
if x == 9:
x = 0
# back to 1
else:
x += 1
but is there a shorter way, that doesn't use a temporary variable, x
?