Skip to content
Home / Fundamentals

Filter in list comprehension using if-else

List comprehensions are a concise way to create a list using a single line of code. They are often used to transform one list into another list by applying a function to each element in the original list. List comprehensions can also include "if" statements to filter elements, allowing you to create a list that only includes certain elements from the original list.

You can also use "if-else" statements in a list comprehension to specify different transformations for elements that meet a certain condition and for those that do not. The syntax for an "if-else" statement in a list comprehension is as follows:

[expression_if_true if condition else expression_if_false for element in iterable]

Here is an example of using an "if-else" statement in a list comprehension to create a list of even and odd numbers:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

even_odd = ['even' if number % 2 == 0 else 'odd' for number in numbers]
print(even_odd)  # Output: ['odd', 'even', 'odd', 'even', 'odd', 'even', 'odd', 'even', 'odd', 'even']

In this example, the list comprehension iterates over the elements in the numbers list and assigns the string 'even' to each element if it is divisible by 2, and 'odd' if it is not. The resulting list even_odd contains the strings 'even' and 'odd' depending on whether the corresponding element in the numbers list is even or odd.

You can also use more complex expressions in the "if" and "else" clauses of an "if-else" statement in a list comprehension. For example, consider the following code that converts a list of Celsius temperatures to Fahrenheit:

celsius = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]

fahrenheit = [(9/5)*c + 32 for c in celsius]
print(fahrenheit)  # Output: [32.0, 50.0, 68.0, 86.0, 104.0, 122.0, 140.0, 158.0, 176.0, 194.0]

In this example, the list comprehension iterates over the elements in the celsius list and applies the formula for converting Celsius to Fahrenheit to each element.

You can also nest list comprehensions inside each other to create more complex transformations. For example, consider the following code that flattens a list of lists:

list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

flattened_list = [element for sublist in list_of_lists for element in sublist]
print(flattened_list)  # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

In this example, the outer list comprehension iterates over the sublists in list_of_lists, and the inner list comprehension iterates over the elements in each sublist and adds them to the flattened list.

List comprehensions with "if-else" statements can be a concise and efficient way to transform and filter lists in Python. However, it's important to keep in mind that list comprehensions can become difficult to read if they are too complex, so it's a good idea to use them sparingly and only when they make your code more readable.

Here is one more example of using an "if-else" statement in a list comprehension to create a list of squared values for positive numbers and cubed values for negative numbers:

numbers = [1, -2, 3, -4, 5, -6, 7, -8, 9, -10]

squared_cubed = [number**2 if number > 0 else number**3 for number in numbers]
print(squared_cubed)  # Output: [1, -8, 9, -64, 25, -216, 49, -512, 81, -1000]

In this example, the list comprehension iterates over the elements in the numbers list and assigns the squared value to each element if it is positive, and the cubed value if it is negative.