iterating over a list as if it were a circular list

Sven svenito at gmail.com
Thu Mar 7 10:34:46 CET 2013


On 7 March 2013 09:31, Chris Rebert <clp2 at rebertia.com> wrote:

> On Mar 7, 2013 1:24 AM, "Sven" <svenito at gmail.com> wrote:
> >
> > I was wondering what the best approach for the following might be.
> >
> > Say you have a list P of points and another list N of other items. You
> can always assume that
> >
> > len(N) <= len(P)
> >
> > Now I would like to iterate over P and place one N at each point.
> However if you run out of N I'd like to restart from N[0] and carry on
> until all the points have been populated.
>
> Untested due to the late hour:
>
> import itertools
>
> for p, n in itertools.izip(P, itertools.cycle(N)):
>     # do whatever
>

I knew there was a more sensible way to do it. Thanks.




-- 
./Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130307/85e05ecb/attachment.html>


More information about the Python-list mailing list