Python List Comprehension — You know about it right?

Jerry Okafor
3 min readJul 15, 2021

--

I have been a student of Machine Learning for a few months now, the reason why I am loving Python. One of the interesting syntaxes of python that I like and always use whenever possible is List Comprehension.

What is List Comprehension?

List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.

This is equivalent to a multi level for loop with or without a conditional check at every stage. List comprehension is not only short, it is sweet and intuitive and can be. they are written in a single line. One quick issue is that things need to be clarified when you have nested list comprehension. We shall start with a simple example and move to more complex ones to understand how this works and how it can be interpreted.

The simplest syntax is: [x for x in collection]

Given a collection of chars all in uppercase we want a new collection with all the original contents all in lowercase.

To achieve this is using the traditional for loop, consider the snippet below:

chars = ["A", "B", "C", "D", "E", "Z"]

chars_lower = []

for c in chars:
chars_lower.append(c.lower())

print(chars_lower) # ['a', 'b', 'c', 'd', 'e', 'z']

Using the sweet and short List comprehension, the 3 lines of code can be replace with 1 line of code as shown below:

chars = ["A", "B", "C", "D", "E", "Z"]
chars_comp = [c.lower() for c in chars]

print(chars_comp) # ['a', 'b', 'c', 'd', 'e', 'z']

Let's take another example with to demonstrate nested list comprehension:

Given a collection of sentences:

sentences = ["New Jersey is sometimes quiet during autumn , and it is snowy in April.",
"The united states is usually chilly during July , and it is usually freezing in November."
"California is usually quiet during march , and it is usually hot in June.",
"The United States is sometimes mild during June , and it is cold in September."]

We want to get a collection of words all in lowercase.

Using for loop:

word_collection_loop = []

for sentence in sentences:
for word in sentence.split():
word_collection_loop.append(word.lower())

print(word_collection_loop)
#['new', 'jersey', 'is', 'sometimes', 'quiet', 'during', 'autumn', ',', 'and', 'it', 'is', 'snowy', 'in', 'april.', 'the', 'united', 'states', 'is', 'usually', 'chilly', 'during', 'july', ',', 'and', 'it', 'is', 'usually', 'freezing', 'in', 'november.california', 'is', 'usually', 'quiet', 'during', 'march', ',', 'and', 'it', 'is', 'usually', 'hot', 'in', 'june.', 'the', 'united', 'states', 'is', 'sometimes', 'mild', 'during', 'june', ',', 'and', 'it', 'is', 'cold', 'in', 'september.']

Using list comprehension:

word_collection_comp = [word.lower() for sentence in sentences for word in sentence.split()]
print(word_collection_comp)
#['new', 'jersey', 'is', 'sometimes', 'quiet', 'during', 'autumn', ',', 'and', 'it', 'is', 'snowy', 'in', 'april.', 'the', 'united', 'states', 'is', 'usually', 'chilly', 'during', 'july', ',', 'and', 'it', 'is', 'usually', 'freezing', 'in', 'november.california', 'is', 'usually', 'quiet', 'during', 'march', ',', 'and', 'it', 'is', 'usually', 'hot', 'in', 'june.', 'the', 'united', 'states', 'is', 'sometimes', 'mild', 'during', 'june', ',', 'and', 'it', 'is', 'cold', 'in', 'september.']

Building list comprehension:

Since list comprehension can be chained just like multi stage for loop, e.g :

[?? for b in a for c in b for d in c ... for z in y]

similar to:

for b in a:
for c in b:
for d in c:
...
for z in y:
# return z

I always approach it by masking the output which in this case is z with ?? and finally computing the what it is after the last stage just like in a normal for loop. In other words, the best place to start building/interpreting List Comprehension is from the for statement. So that in the end, ?? becomes z .

Thanks for reading!

I hope that you found this useful. For more updates, please follow me on twitter. Catch you next time.

--

--

Jerry Okafor

Passionate Software Engineer — iOS, Android and Flutter | Machine Learning | Reader