Re: Iterating a sequence two items at a time

2010-05-11 Thread Ulrich Eckhardt
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(

Re: Iterating a sequence two items at a time

2010-05-11 Thread Bruno Desthuilliers
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]): ...

Re: Iterating a sequence two items at a time

2010-05-11 Thread Chris Rebert
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

Iterating a sequence two items at a time

2010-05-11 Thread Ulrich Eckhardt
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 cod