[Tutor] map vs. list comprehension
Kent Johnson
kent37 at tds.net
Tue Feb 14 21:55:50 CET 2006
Michael Broe wrote:
> I read somewhere that the function 'map' might one day be deprecated
> in favor of list comprehensions.
>
> But I can't see a way to do this in a list comprehension:
>
> >>> map (pow, [2, 2, 2, 2], [1, 2, 3, 4])
> [2, 4, 8, 16]
>
> Is there a way?
The current plan is to drop map() in Python 3.0 which is a "hypothetical
future release of Python".
http://www.python.org/peps/pep-3000.html
List comps aren't so handy as a replacement for map() with a function of
more than one argument. You have to use zip() to convert the separate
arg lists to a single list of arg tuples:
>>> [ pow(x, y) for x, y in zip([2, 2, 2, 2], [1, 2, 3, 4]) ]
[2, 4, 8, 16]
or if you prefer
>>> [pow(*args) for args in zip([2, 2, 2, 2], [1, 2, 3, 4])]
[2, 4, 8, 16]
Kent
More information about the Tutor
mailing list