Cycle around a sequence

Chris Angelico rosuav at gmail.com
Thu Feb 9 11:34:45 CET 2012


On Thu, Feb 9, 2012 at 7:33 PM, Peter Otten <__peter__ at web.de> wrote:
> Chris Angelico wrote:
>
>> def cycle(seq,n):
>>         seq=iter(seq)
>>         lst=[next(seq) for i in range(n)]
>>         try:
>>                 while True: yield next(seq)
>>         except StopIteration:
>>                 for i in lst: yield i
>
> I think that should be spelt
>
> def cycle2(seq, n):
>    seq = iter(seq)
>    head = [next(seq) for i in range(n)]
>    for item in seq:
>        yield item
>    for item in head:
>        yield item

Thanks, yeah, don't know what I was thinking :) Too much C work lately!

ChrisA



More information about the Python-list mailing list