steve+comp.lang.python at pearwood.info
Mon Feb 14 22:58:07 EST 2011
On Tue, 15 Feb 2011 14:10:38 +1100, Ben Finney wrote:
> Andrea Crotti <andrea.crotti.0 at gmail.com> writes:
>> I work on emacs with flymake activated and pylint, pyflakes and pep8
>> running in background to notify for some style problems.
>> Now there are at a couple of pylint warnings which I don't understand
>> 1. Warning (W, filter_enums): Used builtin function 'map' [2 times]
>> what is the problem with using map and other builtin functions?
> The ‘map’ builtin is deprecated;
I don't believe it is. Do you have any evidence for this claim?
> using a list comprehension is neater and more efficient.
The first is a matter of opinion, the second is demonstrably untrue.
Testing in Python 3.1, there is no significant difference when the
function is a pure Python function, although map is slightly faster:
>>> from timeit import Timer
>>> t1 = Timer('[f(x) for x in range(1000)]', 'def f(x): return x+1')
>>> t2 = Timer('list(map(f, range(1000)))', 'def f(x): return x+1')
If the function is a built-in written in C, map can be significantly
>>> t1 = Timer('[len(s) for s in "a"*1000]', '')
>>> t2 = Timer('list(map(len, "a"*1000))', '')
The only time list comps are faster is if you have a expression which can
be executed in-line in the comprehension, but needs to be written as a
Python function in map. And even then, the difference is just a
multiplicative constant, not a Big Oh difference.
More information about the Python-list