Map() vs List Comprehension
Here’s an example:
xs = list(range(10)) list_1 = list(map(lambda x: x*x, xs)) list_2 = [x*x for x in xs] print(list_1 == list_2) # True
list_2 contain the same data: a series of squared values
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81].
The code shows two ways to achieve the same result. Either you can use
map(function, list) and convert the resulting map object to a list or you iterate over each item with a list comprehension. Both ways lead to the same result, thus the output is
So list comprehension is more readable and shorter. But which option is faster?
Speed Benchmark: Map Function vs List Comprehension
The Finxter email course member Wlad asked me the following excellent question:
“Which one is faster from the perspective of the computation? Btw: I like the second one more because it’s more explicit.”
Let’s test both commands against each other using Python’s convenient timeit package:
import timeit xs = list(range(10)) exec_1 = 'list(map(lambda x: x*x, ' + str(xs) + '))' exec_2 = '[x*x for x in ' + str(xs) + ']' time_1 = timeit.timeit(exec_1, number=10000) time_2 = timeit.timeit(exec_2, number=10000) print("Latency (s) map function:\t" + str(time_1)) print("Latency (s) list comprehension:\t" + str(time_2)) ''' Latency (s) map function: 0.011982152000000024 Latency (s) list comprehension: 0.004821852999999987 '''
Try playing with the code yourself in our interactive Python shell:
We create two commands
exec_2. The former is the map function statement from above code snippet. The latter is the list comprehension version of it. We execute both 10,000 times.
The output shows that the map function is 3 times slower than the list comprehension statement!
But what if we increase the number of elements involved (and, thus, the computational complexity for both operations)?
import timeit xs = list(range(10000)) exec_1 = 'list(map(lambda x: x*x, ' + str(xs) + '))' exec_2 = '[x*x for x in ' + str(xs) + ']' time_1 = timeit.timeit(exec_1, number=10000) time_2 = timeit.timeit(exec_2, number=10000) print("Latency (s) map function:\t" + str(time_1)) print("Latency (s) list comprehension:\t" + str(time_2)) ''' Latency (s) map function: 8.007580283 Latency (s) list comprehension: 3.8910019489999996 '''
Although the relative difference between both decreased, the map function still takes twice as long as the list comprehension statement. The reason for the relative improvement of the map function is that the constant overhead of creating, managing, and calling the
map() function decreases in importance considering the larger weight of the list
xs‘s size with 10,000 elements.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.