Get startedGet started for free

What is a list comprehension?

1. What is a list comprehension?

Our next question is: what is a list comprehension? Since it's a very specific feature of Python, this question most likely can be asked on an interview.

2. List comprehension

List comprehension is a special way to define a list. For example, let's consider a list like this. What are the ways to create it besides specifying its items directly? One approach is to create a for loop and fill the empty list with new items. But there is a more elegant way.

3. List comprehension

and it is a list comprehension. What we can do is to iterate through a range object

4. List comprehension

within square brackets

5. List comprehension

while specifying an output element for each input.

6. List comprehension

At the end we assign our expression to a variable. Let's check the output. It looks exactly as we want.

7. Summing up

To sum up, a list comprehension is defined

8. Summing up

by an iterable object

9. Summing up

and an operation on each element from this object. But there is even more. List comprehensions can have conditions.

10. List comprehension with condition

Let's define another way to create our list. We can observe that the list we want to get contains only even numbers up to 10. So, we can achieve our goal by taking all the numbers from 1 to 10

11. List comprehension with condition

and considering only those that are divisible by 2. For this case, list comprehension with condition can be used.

12. Adding a condition

Let's consider this list comprehension. So far, the resulting list contains the numbers from 1 to 10. But you can make a small modification

13. Adding a condition

by adding a condition. In this case we check each element to be divisible by 2 and if so, we include it in the list we create. If we print our newly created list, we get what we want again.

14. More examples

Let's go through one more example. We are given a text. Let's create a list that contains the length of each lowercased word. The output should look like this.

15. More examples

To get it, first we have to iterate through each word in the text.

16. More examples

Then, we add the condition to check if we have a lowercased word.

17. More examples

Finally, we specify what we want to get as an output element. Let's inspect our result: we got what we expected!

18. Multiple loops

List comprehensions can be built with multiple loops. Let's consider these two lists. How could we create a list containing all the possible pairs?

19. Iterating through multiple loops

We start by iterating through the numbers.

20. Iterating through multiple loops

But then we add the iteration through the letters.

21. Iterating through multiple loops

Finally, we insert the output expression. The result is the list of tuples each representing a pair.

22. Deeper look

Although this representation seems quite complicated, when we unwrap it as an ordinary code, it will look this way.

23. Deeper look

Notice that the internal loop is the one on the right side in our list comprehension.

24. Deeper look

And the external loop is on the left side in our list comprehension.

25. Adding square brackets

Let's change a little bit the list comprehension we defined.

26. Adding square brackets

Let's enclose our output expression and the left-most loop in square brackets. Such a small change results in a list of lists! What happens?

27. Adding square brackets

Let's rewrite our list comprehension as an ordinary code. It will look something like this.

28. Adding square brackets

Notice that now the internal loop corresponds to the one on the left side in our list comprehension.

29. Adding square brackets

And the external loop corresponds to the one on the right side.

30. Swap numbers and letters

What happens if we swap numbers and letters?

31. Swap numbers and letters

Here it is. We get completely different output!

32. Difference between list comprehensions

To sum up, we have to take care how we define nested list comprehensions. The output can be substantially different!

33. Let's practice!

Now it's time to exercise and build some list comprehensions yourself!