Using elif and if-else in a List comprehension in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
6 min

banner

# Table of Contents

  1. Using elif statement in a List comprehension in Python
  2. List comprehension with if-else statement in Python

# Using elif statement in a List comprehension in Python

To use an elif statement in a list comprehension:

  1. Use an if statement to check for a condition.
  2. Use an else statement to implement an elif clause.
  3. Use a second else statement to return a value if neither condition is met.
main.py
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']

using elif statement in list comprehension

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.

The 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.

main.py
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.

main.py
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)

implementing the list comprehension in for loop

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.

main.py
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.

The 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.

main.py
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.

main.py
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 you only have an if statement, specify it at the end

If you only have an if statement in a list comprehension, make sure to specify it at the end.

main.py
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]

if you only have if statement specify it at the end

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.

main.py
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.

# List comprehension with if-else statement in Python

To use a list comprehension with if-else:

  1. Specify the if statement first in the list comprehension.
  2. Specify the else statement after the if.
  3. Iterate over a collection in the list comprehension.
main.py
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]

list comprehension with if else statement

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

The syntax for using if-else in a list comprehension is as follows.

main.py
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.

main.py
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.

main.py
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.

main.py
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.

The 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.

main.py
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]
You can also use an if with multiple else statements in a list comprehension to implement an elif condition.
main.py
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.

The condition checks if the item is equal to 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.

main.py
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.

main.py
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.

Otherwise, the 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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev