TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Follow publication

Image created by the author

Member-only story

Python List Comprehension Is Not Just Syntactic Sugar

Christopher Tao
TDS Archive
Published in
8 min readApr 8, 2024

--

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?

1. A Simple Performance Comparison

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.

--

--

TDS Archive
TDS Archive

Published in TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Christopher Tao
Christopher Tao

Written by Christopher Tao

👁️ 4M+ Reads🏆19k+ Followers🥇Top 50 Writer👨‍🎓PhD💻Data & Analytics Leader 🤝LinkedIn https://www.linkedin.com/in/christopher-tao-5717a274/

Responses (8)

Write a response