cyclic iterators ?

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Fri Mar 2 20:49:13 EST 2007


Tool69 a écrit :
> Hi,
> 
> Let say I've got a simple list like my_list = [ 'a', ',b', 'c' ].
> We can have an iterator from it by k =  iter( my_list), then we can
> access each of her (his ?) element by k.next(), etc.
> 
> Now, I just wanted k to have the following cyclic behaviour (without
> rising the ) :
> 
> 
>>>k.next() 
> 'a'
>>>k.next()
> 'b'
>>>k.next()
> 'c'
>>>k.next()     -> not raising StopIteration error
> 'a'
>>>k.next()
> 'b'
> etc.
> 

> I've tried something like this to have a cyclic iterator without
> sucess:
> 
(snip code)


> I missed something, but I don't know what exactly.

from itertools import cycle

HTH



More information about the Python-list mailing list