Python list comprehension using if-else

In this Python tutorial, we will learn python list comprehension using if-else, and also we will cover these topics:

  • Python list comprehension using if statement
  • Python list comprehension using if without else
  • Python list comprehension using nested if statement
  • Python list comprehension using multiple if statement
  • Python list comprehension with if-else
  • Python list comprehension using nested for loop
  • Python list comprehension transposes rows and columns

Python list comprehension using if statement

Here, we can see list comprehension using if statement in Python.

In this example, I have taken a variable as num and I have used for loop for iteration and assigned a range of 10, and if condition is used as if i%2==0. To get the output, I have used print(num).

Example:

num = [i for i in range(10) if i%2==0 ]
print(num)

In the output, you can see the numbers in the form of a list up to the range of 10. You can refer to the below screenshot for the output.

Python list comprehension using if statements
Python list comprehension using if statements

Python list comprehension using if without else

Now, we can see list comprehension using if without else in Python.

In this example, I have taken a variable as num, The num = [i for i in range(10) if i>=5] is used and for iteration, I have used for loop and assigned a range of 10 and then if condition is used as if>=5. To get the output I have used print(num).

Example:

num = [i for i in range(10) if i>=5]
print(num)

The output is in the form of a list where you can see the numbers in the list which are greater than or equal to 5. You can refer to the below screenshot for the output.

Python list comprehension using if without else
Python list comprehension using if without else

Python list comprehension using nested if statement

Now, we can see list comprehension using nested if statement in Python.

  • In this example, I have taken a variable num. The num = [i for i in range(50) if i%2==0 if i%3==0 if i%3==0] is used. For iteration, I have used for loop
  • And assigned a range of 50 and multiple if conditions are used as if i%2==0 if i%3==0 if i%3==0, to print the numbers I have used print(num).

Example:

num = [i for i in range(50) if i%2==0 if i%3==0 if i%3==0]
print(num)

Here, the numbers which satisfy all the three if conditions from the given range will be the output in the list format. The below screenshot shows the output.

Python list comprehension using nested if statement
Python list comprehension using nested if statement

Python list comprehension using multiple if statement

Now, we can see list comprehension using multiple if statement in Python.

  • In this example, I have taken a variable = num, Here, the for loop is used for iterations, and assigned a range of 30.
  • The multiple if statements are used as num = [i for i in range(30) if i>=2 if i<=25 if i%4==0 if i%8==0]. To print the numbers I have used print(num).

Example:

num = [i for i in range(30) if i>=2 if i<=25 if i%4==0 if i%8==0]
print(num)

The number from the given range which satisfys all the multiple if conditions are printed as the output in the list format. The below screenshot shows the output.

Python list comprehension using multiple if statement
Python list comprehension using multiple if statement

Python list comprehension with if-else

Here, we can see list comprehension with if else in Python.

  • In this example, I have a variable as fruits and the if-else condition is used as i%3==0, if the condition is true then the result will be mango else orange.
  • Here, for loop is used for iteration, and to get the output I have used print(fruits).

Example:

fruits = ["mango" if i%3==0 else "orange" for i in range(10)]
print(fruits)

We can see the output as mango when the if condition is true and orange when the else condition is true in the list format as the output. You can refer to the below screenshot for the output.

Python list comprehension with if-else
Python list comprehension with if-else

Python list comprehension using nested for loop

Now, we can see list comprehension using nested for loop in Python.

  • In this example, multiple for loops are used. The range is given from 2 to 4, and for loop is used for iteration.
  • The first for loop is taken as for i in range(2,4): and another for loop as for j in range(1,5):
  • The addition operation is performed for these two loops as a print(f”{i}+{j}={i+j}”).
  • To print the added numbers I have used print(f”{i}+{j}={i+j}”).

Example:

for i in range(2,4):
        for j in range(1,5):
               print(f"{i}+{j}={i+j}")

In the output, you can see the numbers are added from the given range. You can refer to the below screenshot for the output.

Python list comprehension using nested for loop
Python list comprehension using nested for loop

Python list comprehension transposes rows and columns

Here, we can see list comprehension transposes rows and columns in Python.

  • In this example, I have taken a list as list = [[2,4,6,8]] and to transpose this matrix into rows and columns. I have used for loop for iteration.
  • I have taken matrix = [[row[i] for row in list] for i in range(4)] and range is given up to 4.
  • To get the matrix output, I have used print(matrix).

Example:

list = [[2,4,6,8]]
matrix = [[row[i] for row in list] for i in range(4)]
print(matrix)

We can see the four matrices as the output in the below screenshot.

Python list comprehension transposes rows and columns
Python list comprehension transposes rows and columns

You may like the following Python tutorials:

In this tutorial, we have learned about Python list comprehension using if-else, and also we have covered these topics:

  • Python list comprehension using if statement
  • Python list comprehension using if without else
  • Python list comprehension using nested if statement
  • Python list comprehension using multiple if statement
  • Python list comprehension with if-else
  • Python list comprehension using nested for loop
  • Python list comprehension transposes rows and columns