Loop over list of pairs

sismex01 at hebmex.com sismex01 at hebmex.com
Thu Jun 5 10:40:49 EDT 2003


> From: Kevin Carlson [mailto:khcarlso at bellsouth.net]
> Sent: Thursday, June 05, 2003 9:41 AM
> 
> Thomas Güttler wrote:
> 
> >Hi!
> >
> >What is the prefered way of loop over
> >a list like this?
> > mylist=[1, "one", 2, "two", 3, "three"]
> >
> 
> How about this:
> 
> for i in range(0, len(mylist), 2) :
>   print mylist[i], mylist[i+1]  # or whatever you want to do..
> 
> HTH,
> 
> Kevin
>

How about this:

from __future__ import generators

def Pairs(iterable):
   """Returns pairwise iterator"""
   I = iter(iterable)
   while 1:
      yield I.next(), I.next()

for p in Pairs(mylist):
   print p

--gca

--
Advertencia:La informacion contenida en este mensaje es confidencial y
restringida, por lo tanto esta destinada unicamente para el uso de la
persona arriba indicada, se le notifica que esta prohibida la difusion de
este mensaje. Si ha recibido este mensaje por error, o si hay problemas en
la transmision, favor de comunicarse con el remitente. Gracias.





More information about the Python-list mailing list