Iterating through two lists

Oren Tirosh oren-py-l at hishome.net
Fri May 24 10:41:12 EDT 2002


On Fri, May 24, 2002 at 11:56:54AM +0200, 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").

The following are expressions (not statements) with the side effect 
of achieving what you ask for:

List comprehension abuse:
   [a.f(b) for a,b in zip(x,y)]

Functional abuse:
   map(lambda a,b: a.f(b), x, y)

Iterator abuse:
   list(iter(lambda X=iter(x).next, Y=iter(y).next: X().f(Y()), type))

Oh dear... what have I done?

	Oren





More information about the Python-list mailing list