Iterating a sequence two items at a time

Discussion in 'Python' started by Ulrich Eckhardt, May 11, 2010.

  1. Hi!

    I have a list [1,2,3,4,5,6] which I'd like to iterate as (1,2), (3,4),
    (5,6). I can of course roll my own, but I was wondering if there was
    already some existing library function that already does this.


    def as_pairs(seq):
    i = iter(seq)
    yield (i.next(), i.next())

    Question to this code: Is the order of the "i.next()" calls guaranteed to be
    from left to right? Or could I end up with pairs being switched?

    Thanks!

    Uli

    --
    Sator Laser GmbH
    Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
     
    Ulrich Eckhardt, May 11, 2010
    #1
    1. Advertising

  2. Ulrich Eckhardt

    Chris Rebert Guest

    On Tue, May 11, 2010 at 12:09 AM, Ulrich Eckhardt
    <> wrote:
    > Hi!
    >
    > I have a list [1,2,3,4,5,6] which I'd like to iterate as (1,2), (3,4),
    > (5,6). I can of course roll my own, but I was wondering if there was
    > already some existing library function that already does this.


    When a problem involves iteration, always check the `itertools` module
    in the std lib.
    >From the module docs's recipe section

    (http://docs.python.org/library/itertools.html#recipes):

    import itertools
    def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return itertools.izip_longest(fillvalue=fillvalue, *args)

    >>> # Let's try it out.
    >>> list(grouper(2, [1,2,3,4,5,6]))

    [(1, 2), (3, 4), (5, 6)]
    >>> # Success!


    > def as_pairs(seq):
    >    i = iter(seq)
    >    yield (i.next(), i.next())
    >
    > Question to this code: Is the order of the "i.next()" calls guaranteed to be
    > from left to right? Or could I end up with pairs being switched?


    Pretty sure left-to-right is guaranteed; see http://bugs.python.org/issue448679
    Also, if you're using Python 2.6+, the line should be:
    yield (next(i), next(i))
    See http://docs.python.org/library/functions.html#next

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, May 11, 2010
    #2
    1. Advertising

  3. Ulrich Eckhardt a écrit :
    > Hi!
    >
    > I have a list [1,2,3,4,5,6] which I'd like to iterate as (1,2), (3,4),
    > (5,6). I can of course roll my own, but I was wondering if there was
    > already some existing library function that already does this.




    >>> l = range(10)
    >>> for x, y in zip(l[::2], l[1::2]):

    .... print x, y
    ....
    0 1
    2 3
    4 5
    6 7
    8 9

    SimplestThingThatCouldPossiblyWork(tm) - but might not be the most
    efficient idiom, specially with large lists...
     
    Bruno Desthuilliers, May 11, 2010
    #3
  4. Ulrich Eckhardt wrote:
    > I have a list [1,2,3,4,5,6] which I'd like to iterate as (1,2), (3,4),
    > (5,6). I can of course roll my own, but I was wondering if there was
    > already some existing library function that already does this.
    >
    >
    > def as_pairs(seq):
    > i = iter(seq)
    > yield (i.next(), i.next())


    Obviously this code does _not_ do what I want, it must be like this:

    def as_pairs(seq):
    i = iter(seq)
    while True:
    yield (i.next(), i.next())

    Gah!

    Uli

    --
    Sator Laser GmbH
    Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
     
    Ulrich Eckhardt, May 11, 2010
    #4
    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. Ron
    Replies:
    0
    Views:
    466
  2. Mark Fox

    Iterating through Repeater.Items

    Mark Fox, Nov 14, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    8,228
    Mark Fox
    Nov 14, 2003
  3. Bryan
    Replies:
    1
    Views:
    812
    Steven Cheng[MSFT]
    Mar 1, 2006
  4. Neal Becker
    Replies:
    1
    Views:
    249
    Alex Martelli
    Sep 2, 2007
  5. carl
    Replies:
    5
    Views:
    2,509
    James Kanze
    Nov 25, 2009
Loading...

Share This Page