Python List Comprehension Is Not Just Syntactic Sugar | by Christopher Tao | Apr, 2024


“Pythonic” is not a good reason. Why is the performance better? When not to use it?

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?
Image created by the author

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.

We will be happy to hear your thoughts

Leave a reply

0
Your Cart is empty!

It looks like you haven't added any items to your cart yet.

Browse Products
Powered by Caddy
Shopping cart