Iterator for circulating a list

BJörn Lindqvist bjourne at gmail.com
Tue Nov 13 10:07:05 EST 2007


On Nov 13, 2007 3:43 PM, Carsten Haese <carsten at uniqsys.com> wrote:
> On Tue, 2007-11-13 at 15:12 +0100, BJörn Lindqvist wrote:
> > L = somelist
> >
> > idx = 0
> > while True:
> >     item = L[idx]
> >     # Do something with item
> >     idx = (idx + 1) % len(L)
> For begin=0 and step=1, itertools.cycle does exactly that. For arbitrary
> offsets or different steps, you'd have to combine cycle with islice.

*slap forehead* Figures.. I actually read the itertools.cycle
documentation but I didn't realize that it was (almost) equivalent to
my code. Thanks for that.


-- 
mvh Björn



More information about the Python-list mailing list