circular iteration

Simon Brunning simon.brunning at gmail.com
Fri Jan 21 11:46:21 EST 2005


On 21 Jan 2005 08:31:02 -0800, Flavio codeco coelho <fccoelho at gmail.com> wrote:
> hi,
> 
> is there a faster way to build a circular iterator in python that by doing this:
> 
> c=['r','g','b','c','m','y','k']
> 
> for i in range(30):
>     print c[i%len(c)]

I don''t know if it's faster, but:

>>> import itertools
>>> c=['r','g','b','c','m','y','k']
>>> for i in itertools.islice(itertools.cycle(c), 30):
... 	print i

-- 
Cheers,
Simon B,
simon at brunningonline.net,
http://www.brunningonline.net/simon/blog/



More information about the Python-list mailing list