Iterating throught 2 lists at the same time

Rajarshi Guha rajarshi at seed.chem.psu.edu
Thu Aug 23 17:16:17 EDT 2001


Hi,
  I have the following construct:

>>> a = ['a','b','c']
>>> b = ['x','y','z']
>>> for i,j in a,b:
...     print i,j
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: unpack list of wrong size


In my actual program the elements of a and b are themselves lists, but I 
think that does'nt  really matter (or does it?)
So, how can I iterate over a and b in 'parallel'  (ie in the first 
iteration I want a[0] and b[0] and so on)

I know I could maintain counters and use the index notation - but it seems 
a little crude for Python :) Are there any, more elegant ways?

-- 
--
Rajarshi Guha



More information about the Python-list mailing list