Loop over list of pairs

Kevin Carlson khcarlso at bellsouth.net
Thu Jun 5 16:41:07 CEST 2003


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






More information about the Python-list mailing list