How to use map

Alex Martelli aleaxit at yahoo.com
Fri Mar 23 12:26:02 EST 2001


<apardon at trout.vub.ac.be> wrote in message
news:slrn9bmq1g.pav.apardon at trout.vub.ac.be...
    [snip]
> I was wondering should k, l and m now be lists of lists
> of the same length and all sublists having the same
> length too, if it would be possible to eliminate the
> use of any loop and get the same result as
>
>   for i in range(len(m)):
>     for j in range(len(m[0])):
>       m[i][j] = f( k[i][j] , l[i][j] )

Depends on what you mean by 'loop' -- you can do
it with list comprehensions quite easily, of course:

m = [ [ f(ak, al) for ak, al in zip(lak, lal) ]
            for lak, lal in zip(k, l)
    ]

but one might argue that these _are_ just loops by
some other name (as, of course, another might
argue of map & friends).


Alex






More information about the Python-list mailing list