Re: iterating over a list as if it were a circular list

Discussion in 'Python' started by Sven, Mar 7, 2013.

  1. Sven

    Sven Guest

    On 7 March 2013 09:31, Chris Rebert <> wrote:

    > On Mar 7, 2013 1:24 AM, "Sven" <> 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
    Sven, Mar 7, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sven
    Replies:
    1
    Views:
    143
    Roy Smith
    Mar 7, 2013
  2. Sven
    Replies:
    3
    Views:
    155
    Steven D'Aprano
    Mar 8, 2013
  3. Chris Angelico
    Replies:
    0
    Views:
    118
    Chris Angelico
    Mar 7, 2013
  4. Chris Rebert
    Replies:
    0
    Views:
    116
    Chris Rebert
    Mar 7, 2013
  5. Chris Rebert
    Replies:
    0
    Views:
    125
    Chris Rebert
    Mar 7, 2013
Loading...

Share This Page