Python List Comprehension Using IF Condition

In Python, different data are represented using multiple data types. The “list” data type is also used in Python to store the elements in the sequence. Every element in the list is separated by a (,) comma. List comprehension creates a list from another list or string concisely. This method can reduce multiple lines of code into a single line and also increase the code’s functionality.

This post provides a deep insight into the core concepts of Python List comprehension using the “IF condition”. The following contents are discussed in this post:

So let’s get started!

Python List Comprehension Using If Condition

List comprehension functionality increases when we use it with the help of “if” or “if-else” conditions. We can filter out more data by applying multiple conditions on the list. The list comprehension using the “if statement” has an expression used to evaluate the list according to the condition applied by the developer. The syntax of python list comprehension using the “if condition” is given below:

variable= [ expression for element in list if condition ]

In the above syntax, the Python List Comprehension is enclosed inside the square bracket “[ ]”. The “expression” value is used for the operation which is performed on each value of the “variable” of the “list” or another iterable object. The “if statement” is used to apply the condition on the list.

Example 1: Python List Comprehension Using if Condition

Python list comprehension using the if condition operates on the list to create a new list by completing the condition. Let’s understand it with an example of code given below:

Code:

#python list comprehension using if condition
val_1 = [10,5,4,6,15,11,17,20]
number = [value for value in val_1 if value%2==0 ]
print(number)

In the above code:

  • The list value is initialized in the  “val_1” variable.
  • The “list comprehension” method is applied to list “val_1” and creates a new list based on the condition of “if-statement”.
  • According to the condition, all the values of a list divided by “2” equal to zero are printed on the screen.

Output:

In the above output, a distinct list of numbers is created (that could be divided by “2”).

Example 2: Python List Comprehension Using Nested if Condition

List comprehension is also used with nested if conditions. We solved more complex problems by single expression with double if condition. If one of the conditions does not satisfy, then the expression looks for another condition. By applying these different conditions to the list, we filtered out the required data. Let’s understand this via the following mentioned code:

Code:

#python list comprehension using nested if condition
val_1 = [10,5,4,6,30,3,15,11,17,20]
number = [value for value in val_1 if value%2==0 if value%3==0 if value>=5]
print(number)

In the above code:

  • The list variable named “val_1” is initialized.
  • The “list comprehension” along with multiple “if statements” creates a new list according to the condition.
  • If any value of the list does not satisfy any if condition then it will not be printed in the output.

Output:

The output shows the number which satisfies the if-condition of list comprehension.

Example 3: Python List Comprehension Using If-else Condition

Python list comprehension makes the program more concise and useful. If the condition is not satisfied, then the expression looks for the else condition; it performs multiple operations with only a single line expression. Let’s understand it with an example of code given below:

Code:

#python list comprehension using if-else condition
val_1 = [10,5,4,6,30,3,15,11,17,20]

number = ["Even" if value%2==0 else "Odd" for value in val_1]
print(number)

In the following mentioned code:

  • The list variable named “val_1” is initialized.
  • The “list comprehension” is utilized here with the “if-else” condition.
  • If the value of the list divided by “Two” is equal to “zero” then “Even” is returned on the screen otherwise, list comprehension prints the “else” expression named “Odd”.

Output:

The output shows “Even” and “Odd” according to the “if-else” condition.

That’s all from the List comprehension using the If condition in Python.

Conclusion

In Python, “list comprehension” using the “if-condition” is used to create the list and perform an operation on the existing elements of a list elegantly and concisely. It is a one-liner code that can operate multiple lines. Python list comprehension is a fast way to create and filter the data using “if-else” conditions. This write-up elaborates on “Python list” comprehension using“if condition” with numerous examples.