8 Levels of Using List Comprehension in Python
Introduction
The list comprehension is a very Pythonic technique and able to make your code very elegant. However, its syntax is a little confusing especially for new learners and programmers who come from other languages. I read lots of materials about it, but to be honest, none of them can perfectly show a whole picture and how powerful and beautiful the list comprehension is. Therefore, here comes this article. 😊
This post will demonstrate 8 levels of using the list comprehension from elementary to profound. After understanding all the 8 levels, mastering the list comprehension will be just a piece of cake. 🍰
Level 0: Know the Template of the List Comprehension
First of all, we should know the basic syntax:
Every list comprehension should abide by the template as following:
my_list=[ expression for item in iterable (if condition) ]
It’s very neat and simple. Just two square brackets including three key components:
- A for-loop to iterate an iterable
- An expression to deal with the item
- An optional
if
condition
Next, let’s take a look at how to write ingenious programs with this simple template.
Level 1: Just Replace For-loops
An intuitive scenario is to replace a for-loop by an one line code:
Compared the following for-loop version implementation, this is already a big step to a Pythonic and elegant program.
Actually, all iterables in Python can be used in a list comprehension. Another example:
As the above example shown, we can get the maximum value of each row in a matrix with only one line of code.
Level 2: Use the If Condition Smartly
The if
statement is an optional condition in the list comprehension. It will give us lots of convenience if we use it well.
Level 3: Use a More Complex Expression
In the previous examples, we just get items to build a list. Actually, we can apply a more complex expression for items:
Even including an if...else...
statement:
Note: There is one issue that could be confusing if you haven’t really understood the list comprehension template:
The if...else...
statement, also called ternary conditional operator, in the expression is different with the optional if
condition at the last of the list comprehension template. Let’s review the template:
my_list=[ expression for item in iterable (if condition) ]
As the template shown, the last if
condition is one of the components of a list comprehension. We can’t add an else
statement after it since the syntax of list comprehension does not support that.
The expression part can be any expressions as long as it follows the Python expression’s syntax. If we use an if
, there must be an else
as well since it’s the ternary conditional operator syntax of the Python expression.
Level 4: Use Nested For-Loops to Handle Nested Iterables
One list comprehension is not only able to replace one for-loop, it actually can replace nested for-loops.
The above program is equal to:
Which implementation is better? The answer is so obvious. 😃
Of course, we can put more nested for-loops in one list comprehension, but it’s not a good idea. The best practice is never using more than two for-loops in one comprehension for the readability sake.
In addition, we can add the optional if
conditions after any for-loops:
Level 5: Avoid Higher Order Functions for Readability
Python has some higher order functions such as map()
, filter()
and so on. It’s a good habit to always use the list comprehension instead of higher order functions. Because it makes our programs more readable for others. Even the author of Python recommended this practice in his post.
The map()
method can always be replaced:
L = map(func, iterable)
# can be replaced to:
L = [func(a) for a in iterable]
The filter()
method can be converted as well:
L = filter(condition_func, iterable)
# can be converted to
L = [a for a in iterable if condition]
Let’s see an example, the following lists (L1
and L2
) implemented by two different ways have the same result:
Level 6: Use Generator Expressions to Reduce Memory Costs
If we convert the square brackets to parentheses, a list comprehension will become a generator expression.
The generator expression can reduce the memory costs by avoiding producing a full list, because a generator applies lazy evaluation.
Level 7: Understand the Philosophy Behind the List Comprehension
The intuitive reason to use the list comprehension is to make our code more neat and elegant. Furthermore, it’s a good practice of the functional programming paradigm. One of the functional programming philosophy is avoiding control flows. List comprehension can shift programmers’ focus from the control flow to the data collection itself. In other words, it’s a mentally shift from thinking of how a for-loop works to what the list is. It can help you think about the logic of the entire program more easily.
Conclusion
List comprehension is a classic example to show how elegant a Python program can be. After getting familiar with its syntax and using scenarios, your Python programming skills will enter into a new realm.
Thanks for reading!
Relative articles: