If the generated list is large, it can be better to use a generator, so that items are generated one at a time on the fly without creating the whole large list in memory:
from itertools import islice, cycle
def repeat(lst, times):
return islice(cycle(lst), len(lst)*times)
lst = [0, 1, 2, 3, 4, 5]
for item in repeat(lst, 3):
print(item, end=' ')
#0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5
You can still create a list if you want:
print(list(repeat(lst, 3)))
# [0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5]
How it works: itertools.cycle will cycle on lst
indefinitely, and we keep only the len(lst) * times
first items using itertools.islice