Iterating throught 2 lists at the same time

Mark Day mday at apple.com
Thu Aug 23 17:32:58 EDT 2001


In article <9m3rmg$8u55$1 at ID-91520.news.dfncis.de>, Rajarshi Guha
<rajarshi at seed.chem.psu.edu> wrote:

>   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)

>>> for i,j in zip(a,b):
...   print i,j
... 
a x
b y
c z



More information about the Python-list mailing list