I guess you must find out there are too many articles telling us to use the list comprehension rather than a for-loop in Python. I have seen too many. However, I’m kind of surprised that there was almost no article explaining why.
People like me won’t be simply convinced by the reasons like “Pythonic” or “readability”. Instead, these “reasons” actually give Python newbies a wrong impression. That is, the Python list comprehension is just a syntactic sugar.
In fact, the list comprehension is one of the great optimisations in Python. In this article, let’s have a deep dive into the mechanisms under the hood. You will get answers to the following questions.
- What is list comprehension in Python?
- Why its performance is better than a for-loop generally?
- When we should NOT use list comprehension?
Let’s start from the basic part, which is writing a simple program with for-loop and list comprehension respectively. Then, we can compare the performance.
factor = 2
results = []for i in range(100):
results.append(i * factor)
The code above defines a for-loop that will be executed 100 times. The range(100)
function generates 100 integers and each of them will be multiplied with a factor
. The factor that was defined earlier, is 2.
Now, let’s have a look at the list comprehension version.
factor = 2
results = [i * factor for i in range(100)]
In this example, it’s absolutely much easier and more readable. Let’s run these two equivalent code snippets and compare the performance.
Discover more from reviewer4you.com
Subscribe to get the latest posts to your email.