Python List Comprehension with Syntax and Examples

1. Python List Comprehension

Previously, we discussed about Lists in Python. In that tutorial, we mentioned that we can use a list comprehension to create a list. Today, we’ll expand on Python list Comprehension.

Python List Comprehension

2. Introduction of Python List Comprehension

To get comprehensions back into memory, we’ll take a quick example.

Suppose you want to take the letters in the word ‘anxiety’, and want to put them in a list. Remember that a python string is an iterable. Using a for loop, you would:

>>> mylist=[]
>>> for i in 'anxiety':
>>> mylist

[‘a’, ‘n’, ‘x’, ‘i’, ‘e’, ‘t’, ‘y’]

But with a list comprehension, you can do this in one line:

>>> [i for i in 'anxiety']

[‘a’, ‘n’, ‘x’, ‘i’, ‘e’, ‘t’, ‘y’]

Now that we’ve got your interest, we’ll dig a little deeper.

3. Syntax of List Comprehension in Python

For a python list comprehension, we use the delimiters for a list- square brackets. Inside those, we use a for-statement on an iterable. We’ll take an example.

>>> [i*2 for i in {3,1,2}]

[2, 4, 6]

Here, we return twice of every value in the set {3,1,2} into a new list.

So we guess we can state the syntax for a list comprehension as follows:

[item for item in iterable]

Note that not every loop has an equivalent list comprehension.

4. Python List Comprehension vs Python3 Lambda Expression

All this syntax around returning an expression reminds us of something we’ve learned before. What was it? Yes, python lambda expressions. In Python3 lambda expression, we return an expression’s value based on arguments. Let’s create a Python list comprehension to create a list from a set.

>>> myset={3,1,2}
>>> makelist=lambda i:list(i)
>>> mylist=makelist(myset)
>>> mylist

[1, 2, 3]

Here, we first took a set {3,1,2}. Like you’re aware by now, it rearranges itself as {1,2,3}. Then, we defined a lambda function, and stored it in the variable ‘makelist’. This lambda function takes a value, converts it into a list, and returns it. Finally, we called makelist on the set myset, and stored it in the variable mylist, which now holds a list.

>>> type(mylist)

<class ‘list’>

To do this using the map function instead, we write the following code:

>>> list(map(lambda i:i,myset))

[1, 2, 3]

This code first takes a lambda expression: For each value i, it returns i and maps this on each value in the set myset. Next, it converts this into a python list and prints it.

A list comprehension’s advantage over a lambda function is that it is more readable. Try reading both, and see for yourself.

5. Conditionals in Python List Comprehension

So far, you know that we use a for-statement to declare our intentions. But did you know that it is possible to add a condition to this? This will add only those items to the list that meet the condition (for which the condition is True).

>>> [i for i in range(8) if i%2!=0]

[1, 3, 5, 7]

This code takes the values in range(8), i.e., 0 to 7, and adds the odd values to a list.

a. Nested Conditionals

With a list comprehension, it doesn’t have to be a single condition; you can nest conditions. Here’s an example.

>>> [i for i in range(8) if i%2==0 if i%3==0]

[0, 6]

Let’s see how this works. For integers 0 to 7, it first filters out the elements that aren’t perfectly divisible by 2. For the remaining elements, it keeps only those that are divisible by 3. Finally, it stores these elements in a list, and prints it out.

Remember, this is a nested conditional, not an AND operation of two conditions.

b. if..else in List Comprehension

You can also use an if-else in a list comprehension. Since in a comprehension, he first thing we specify is the value to put in a list, this is where we put our if-else.

>>> ["Even" if i%2==0 else "Odd" for i in range(8)]

[‘Even’, ‘Odd’, ‘Even’, ‘Odd’, ‘Even’, ‘Odd’, ‘Even’, ‘Odd’]

This code stores in a list, for each integer from 0 to 7, whether it is even or odd.

Try using different conditions with this one, and tell us in the comments.

6. Nested List Comprehension Python

Finally, in this tutorial, we will end discussing how to use a Python list comprehension for a nested for-loop.

Let’s take some code to print the tables of numbers 7 and 8. Using regular for-loops, we’d write the following code:

>>> for i in range(7,9):
for j in range(1,11):





















To do this using a python list comprehension, however, we use the following code:

>>> [[i*j for j in range(1,11)] for i in range(7,9)]

[[7, 14, 21, 28, 35, 42, 49, 56, 63, 70], [8, 16, 24, 32, 40, 48, 56, 64, 72, 80]]

We used the for-loop for j as the inner comprehension, because it is the inner loop in the previous code.

Read: The Tremendous Python Career Opportunities in 2018

7. Conclusion

Now that you do better with python list comprehensions, we hope you’ll make good use of it for speed and readability. However, it makes no sense to write a very long and complicated list comprehension. Also, you can write a for-loop for every list comprehension in python, but not you can’t write list comprehensions for very complex for-loops.

Leave a comment

Your email address will not be published. Required fields are marked *