Is there an elegant way to cycle through a list N times via iteration (like itertools.cycle but limit the cycles)?

前端 未结 6 724
执念已碎
执念已碎 2020-12-09 05:56

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

6条回答
  •  无人及你
    2020-12-09 06:26

    import itertools
    itertools.chain.from_iterable(itertools.repeat([1, 2, 3], 5))
    

    Itertools is a wonderful library. :)

提交回复
热议问题