You could e.g. combine itertools.cycle
and .islice
, or just use modulo %
:
>>> from itertools import islice, cycle
>>> lst = ['a', 'was', 'mother']
>>> list(islice(cycle(lst), 10))
['a', 'was', 'mother', 'a', 'was', 'mother', 'a', 'was', 'mother', 'a']
>>> [lst[i % len(lst)] for i in range(10)]
['a', 'was', 'mother', 'a', 'was', 'mother', 'a', 'was', 'mother', 'a']
(Technically, this does not append to an empty list but creates the list in one go, but I assume that's okay.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…