I'd like to cycle through a list repeatedly (N times) via an iterator, so as not to actually store N copies of the list in memory. Is there a built-in or elegant way to do this without writing my own generator?
Ideally, itertools.cycle(my_list) would have a second argument to limit how many times it cycles... alas, no such luck.
import itertools
it = itertools.chain.from_iterable(itertools.repeat([1, 2, 3], 5))
print(list(it))
# [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
Itertools is a wonderful library. :)
As pointed out in the comments, if you are using iterators (instead of lists or similar collections), iterators are exhausted after the first iteration, and so you have to use a slightly different approach to get the expected result. Note that infinite or excessively long iterators can't be reasonably handled, as you would have to cache the result (requiring infinite or excessive amounts of memory).
This may not be the most efficient implementation (tee
stores N copies of the iterable's contents):
import itertools
it = itertools.chain(*itertools.tee(iter([1, 2, 3]), 5))
print(list(it))
# [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]