Python List Comprehension Multiple Conditions
You have already learned previously about list comprehension in Python to create a new list from an existing list. Now we will learn about how to use multiple conditions in list comprehension.
You can think of List comprehension as an avatar of for loop with the special purpose of creating a new list from an existing list with some limitations.
Interestingly, it supports condition and multiple conditions as well just like for loop.
Let's see how conditions & then multiple conditions are used in list comprehension.
List Comprehension with Condition
A simple list comprehension program looks like this:
numbers = [1, 2, 3, 4, 5]
# create a new list with the squares of the numbers
squares = [n * n for n in numbers]
print(squares) # [1, 4, 9, 16, 25]
Now, let's add a condition to this list comprehension program to filter out the odd numbers.
To do this, add an if statement at the end of the list comprehension.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# squares of the even numbers
# single condition in list comprehension
squares = [n * n for n in numbers if n % 2 == 0]
print(squares) # [4, 16, 36, 64, 100]
Here, the if statement is used to filter out the odd numbers from the list.
It is important to note that the if statement is added after the for loop ends in the list comprehension.
Follow the same syntax to add multiple conditions in list comprehension.
List Comprehension with Multiple Conditions
Just like a single condition, you can add multiple conditions in list comprehension as well.
To do this, add multiple if statements at the end of the list comprehension next to each other. The first condition at the end of the list comprehension will be applied first and then the second condition will be applied and so on.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# squares of the even numbers greater than 5
# multiple conditions in list comprehension
squares = [n * n for n in numbers if n % 2 == 0 if n > 5]
print(squares) # [36, 64, 100]
Here, the first if statement is used to filter out the odd numbers from the list and the second if statement is used to filter out the numbers less than or equal to 5.
Here are some more examples of list comprehension with multiple conditions.
Example 1
Using if and else in list comprehension.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# half the even numbers and square the odd numbers
new_list = [n * n if n % 2 == 0 else n / 2 for n in numbers]
print(new_list)
# [0.5, 4, 1.5, 16, 2.5, 36, 3.5, 64, 4.5, 100]
Example 2
Using if, elif and else in list comprehension.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# half the even numbers, square the odd numbers, and cube the numbers greater than 5
new_list = [n * n * n if n > 5 else n * n if n % 2 == 0 else n / 2 for n in numbers]
print(new_list)
# [0.5, 4, 1.5, 16, 2.5, 216, 343, 512, 729, 1000]
Example 3
Using 3 if statements in list comprehension.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# list of even squares between 5 and 9
new_list = [n * n for n in numbers if n % 2 == 0 if n > 5 if n < 9]
print(new_list) # [36, 64]
This is how you can use multiple conditions in list comprehension.
We covered almost all combinations like, nested if, if and else, if, elif and else, and multiple if statements in list comprehension.
Hope you learned something new today.
Happy Learning!😊