cyclic iterators ?

Tool69 kibleur.christophe at gmail.com
Fri Mar 2 20:11:00 EST 2007


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:

def iterate_mylist(my_list):
    k = len((my_list)
    i=0
    while i <= k :
        yield my_list[i]
        i += 1
    i = 0
    yield my_list[0]

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




More information about the Python-list mailing list