Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Iterating a sequence two items at a time

Reply
Thread Tools

Iterating a sequence two items at a time

 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      05-11-2010
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

 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      05-11-2010
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
 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      05-11-2010
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...


 
Reply With Quote
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      05-11-2010
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Iterating a std::vector vs iterating a std::map? carl C++ 5 11-25-2009 09:55 AM
code check for modifying sequence while iterating over it? Neal Becker Python 1 09-02-2007 05:13 PM
Iterating through all the menu/child items in a menu control Bryan ASP .Net 1 03-01-2006 09:38 AM
Iterating through Repeater.Items Mark Fox ASP .Net 1 11-14-2003 12:40 AM
CheckBoxList -- Iterating Through Items Ron ASP .Net 0 07-15-2003 06:51 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57