Converting a list of strings into a list of integers?

Steven D'Aprano steve+comp.lang.python at
Sun Jul 22 19:01:11 CEST 2012

On Sun, 22 Jul 2012 19:20:18 +0300, Jan Riechers wrote:

> "map" works on a list and does commandX (here "int" conversion, use
> "str" for string.. et cetera) on sequenceY, returning a sequence. More
> in the help file.
> And if I'm not completely mistaken, it's also the quicker way to do
> performance wise. But I can't completely recall the exact reason.

The following only applies the standard CPython implementation. Other 
implementations may be different. In particular, PyPy turns everything 
you know about optimizing Python code on its head, and can often approach 
the speed of optimized C code in pure Python.

map is faster than an ordinary for-loop if the function you are applying 
is a builtin like int, str, etc. But if you have to write your own pure-
Python function, the overhead of calling a function negates the advantage 
of map, which is no faster than a for-loop. For example:

results = map(int, sequence)  # calls builtin `int`

hoists the call to int into the fast C layer, instead of the slow Python 
layer, and should be faster than

results = []
for x in sequence:

which runs at the speed of Python. But:

results = map(lambda x: x+1, sequence)  # calls pure Python function

if no faster than a for-loop:

results = []
for x in sequence:

Note: this has *nothing* to do with the use of lambda. Writing the "+1" 
function above using def instead of lambda would give the same results.

List comprehensions are at least as fast as map, since they too hoist the 
calculation into the fast C layer. They have the added advantage that 
they can calculate arbitrarily complex Python expressions in the C layer 
without needing an intermediate function. So:

map(lambda x: x**2 - 3, sequence)

runs more-or-less at the speed of an ordinary for-loop, but the list 
comprehension version:

[x**2 - 3 for x in sequence]

should be faster and doesn't rely on an intermediate function.

So in general, a list comprehension will be no slower than map, and may 
be faster; both will be no slower than a for-loop, and may be faster.

Or at least, this was the case last time I checked.


More information about the Python-list mailing list