fbpx

As a Python developer I have seen lots of different preferences in this topic. These preferences aside, I was set to find which of them is faster in which situations.

TLDR;

If you require a list of results almost always use a list comprehension. If no results are required, using a simple loop is simpler to read and faster to run. Never use the builtin map, unless its more aesthetically appealing for that piece of code and your application does not need the speed improvement.

No Result Required

The first pattern I have seen is the use of Map, List Comprehension vs a standard loop in the case where there is no result required. Therefore the resulting list (returned by python’s map function or list comp) is ignored. For example, the following

for entry in entries:
    process(entry)

vs

map(process, entries)
[process(entry) for entry in entries]

The results are as follows, there were three variations of the test, the first is a single loop (O(n)), second is loop within a loop (O(n2)), and third is O(n3), three loops. The test code can be seen on github.

Result Required

This is the traditional pattern, where the generated list is useful. With this approach the following is how fast each approach is in python. For example

results = []
for entry in entries:
    results.append(process(entry))

vs

results = map(process, entries)
results = [process(entry) for entry in entries]

Same as the previous test, the three variations were tested, O(n), O(n2), and O(n3).

Conclusions

If you require a list of results almost always use a list comprehension. If no results are required, using a simple loop is simpler to read and faster to run. Never use the builtin map, unless its more aesthetically appealing for that piece of code and your application does not need the speed improvement.