Iterating through two lists

Boudewijn Rempt boud at valdyas.org
Fri May 24 06:15:16 EDT 2002


jb wrote:

> I have two lists, x and y with the property len(x) = len(y).
> 
> I should like to achive this (x is a list of class instances)
> 
>   for (a,b) in (x,y): a.f(b)
> 
> Is there a fancy way of doing this or have I to introduce an auxillary
> counter (that is very easy but maybe not very "lispy", that is
> "python-like").
> 

Is this what you want?

>>> a=[1,2,3]
>>> b=['a','b','c']
>>> map(None, a, b)
[(1, 'a'), (2, 'b'), (3, 'c')]
>>> for t in map(None, a, b):
...     print t
...
(1, 'a')
(2, 'b')
(3, 'c')
>>> for i, j in map(None, a, b):
...     print i, j
...
1 a
2 b
3 c
-- 
Boudewijn Rempt | http://www.valdyas.org



More information about the Python-list mailing list