Last updated: Apr 10, 2024
Reading timeยท6 min

To use an elif statement in a list comprehension:
if statement to check for a condition.else statement to implement an elif clause.else statement to return a value if neither condition is met.a_list = [1, 2, 2, 5, 1, 9] new_list = [ 'a' if item == 1 else 'b' if item == 2 else 'c' for item in a_list ] print(new_list) # ๐๏ธ ['a', 'b', 'b', 'c', 'a', 'c']

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.
We used an else statement to implement an elif clause in a list
comprehension.
The if statement checks if the current item is equal to 1 and if the
condition is met, a is returned.
else statement returns another condition. The condition checks if the item is equal to 2. If the condition is met, b is returned.If neither condition is met, c is returned.
In pseudo-code, the syntax looks as follows.
new_list = [ return A if condition_is_met else return B if condition_is_met else return C for item in a_list ]
Notice that the if and else statements are at the beginning of the list
comprehension.
Here is how we would implement the list comprehension from the example using a for loop.
a_list = [1, 2, 2, 5, 1, 9] new_list = [] for item in a_list: if item == 1: new_list.append('a') elif item == 2: new_list.append('b') else: new_list.append('c') # ๐๏ธ ['a', 'b', 'b', 'c', 'a', 'c'] print(new_list)

The for loop from the code sample is equivalent to the list comprehension.
Here is another example of using an elif statement in a list comprehension.
a_list = [1, 2, 2, None, 1, None] new_list = [ 0 if item is None else item if item % 2 == 0 else item + 11 for item in a_list ] print(new_list) # ๐๏ธ [12, 2, 2, 0, 12, 0]
The if statement
checks if the current item is None and if the
condition is met, 0 is returned.
else statement implements another condition. The condition checks if the number divided by 2 has a remainder of 0.If the condition is met, the item is returned, otherwise, the item + 11 is
returned.
The same approach can be used if you only need to implement an if-else statement in a list comprehension.
a_list = [1, 2, 2, None, 1, None] new_list = [ 0 if item is None else item for item in a_list ] print(new_list) # ๐๏ธ [1, 2, 2, 0, 1, 0]
The if statement checks if the item is None. If
the condition is met, 0 is returned, otherwise, the item is returned.
The syntax in pseudo-code looks as follows.
new_list = [ return A if condition_is_met else return B for item in list ]
Notice that the if and else statements are specified at the beginning of the
list comprehension.
if statement, specify it at the endIf you only have an if statement in a list comprehension, make sure to specify
it at the end.
a_list = [1, 2, 2, None, 1, None] new_list = [ item for item in a_list if item is not None ] print(new_list) # ๐๏ธ [1, 2, 2, 1]

Implementing an if-elif-else statement in a list comprehension might make your
code more difficult to read.
If that's the case, simply use a for loop.
a_list = [1, 2, 2, None, 1, None] new_list = [] for item in a_list: if item is None: new_list.append(0) elif item % 2 == 0: new_list.append(item) else: new_list.append(item + 11) print(new_list) # ๐๏ธ [12, 2, 2, 0, 12, 0]
The for loop approach is a bit more verbose but in my opinion, it is more
readable and intuitive.
To use a list comprehension with if-else:
if statement first in the list comprehension.else statement after the if.a_list = [2, 4, 8, 13, 10] new_list = [ item if item > 8 else item + 10 for item in a_list ] print(new_list) # ๐๏ธ [12, 14, 18, 13, 10]

The syntax for using if-else in a list comprehension is as follows.
new_list = [ return A if condition_is_met else return B for item in list ]
The syntax a if condition else b is called
the ternary operator.
site = 'bobbyhadz.com' result = 'a' if len(site) > 1 else 'b' print(result) # ๐๏ธ 'a'
The ternary operator returns the value to the left if the condition is met, otherwise, the value to the right is returned.
If you only have an if statement in your list comprehension, specify the
condition at the end.
a_list = [2, 4, 8, 13, 10] new_list = [ item for item in a_list if item > 8 ] print(new_list) # ๐๏ธ [13, 10]
However, you can't have an if/else statement at the end of a list comprehension as it is an invalid syntax.
Here is another example.
a_list = [2, None, 4, None, 8] new_list = [ item if item is not None else 0 for item in a_list ] print(new_list) # ๐๏ธ [2, 0, 4, 0, 8]
We used a list comprehension to iterate over the list.
if statement checks if the current item is not None and if the condition is met, the item is returned.Otherwise, the else statement returns 0.
The list comprehension is equivalent to the following for loop.
a_list = [2, None, 4, None, 8] new_list = [] for item in a_list: if item is not None: new_list.append(item) else: new_list.append(0) print(new_list) # ๐๏ธ [2, 0, 4, 0, 8]
if with multiple else statements in a list comprehension to implement an elif condition.a_list = [2, 4, 8, 4, 19] new_list = ['a' if item == 4 else 'b' if item == 8 else 'c' for item in a_list] print(new_list) # ๐๏ธ ['c', 'a', 'b', 'a', 'c']
The if statement checks if the current item is equal to 4 and if the
condition is met, the letter a is returned.
The else statement returns another condition.
8. If the condition is met, the letter b is returned, otherwise, the letter c is returned.In pseudo-code, the syntax looks as follows.
new_list = [ return A if condition_is_met else return B if condition_is_met else return C for item in a_list ]
Here is how you'd implement the list comprehension from the previous example
using a for loop.
a_list = [2, 4, 8, 4, 19] new_list = [] for item in a_list: if item == 4: new_list.append('a') elif item == 8: new_list.append('b') else: new_list.append('c') print(new_list) # ๐๏ธ ['c', 'a', 'b', 'a', 'c']
We check if the item is equal to 4 and append a to a new list if the
condition is met.
elif statement checks if the current item is equal to 8. If the condition is met, we append b to the new list.If the conditions aren't met, the else statement runs and we append c to the
list.
Checking for too many conditions in list comprehensions might get more complicated than necessary.
In these cases, you can use a simple for loop for readability purposes.
You can learn more about the related topics by checking out the following tutorials: