iterating over lists

John Hunter jdhunter at nitace.bsd.uchicago.edu
Sat Jun 2 11:04:06 EDT 2001


I have three lists of equal size: list1, list2 and list3.

What I want but doesn't work:

  for (x1, x2, x3) in (list1, list2, list3):
     print x1, x2, x3

I know I can do it by keeping a index count variable: 

     N = len(list1)
     for i in range(1,N):
         print list1[i], list2[i], list3[i]

but am wondering if there is way to create a tuple of named variables
as in the (x1, x2, x3) example above.


One final question:  If I do:

     for i in range( 1, len(list1) ):
         print list1[i], list2[i], list3[i]

I assume the len(list1) call will only be evaluated once.  Is this
correct?

Thanks,
John Hunter 



More information about the Python-list mailing list