Iterating a sequence two items at a time
Ulrich Eckhardt
eckhardt at satorlaser.com
Tue May 11 04:43:23 EDT 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
More information about the Python-list
mailing list