Open In App

Python List Comprehension Using If-Else

Last Updated : 14 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

List comprehension in Python is a way to make the elements get added to the list more easily. We can use if-else with List Comprehension which makes the code smaller and more modular instead of using long if-else conditions making it very unstructured. In this article, we will see how we can use list comprehension with Python if-else.

List Comprehension Using If-Else in Python

Below are some of the examples by which we can understand about list comprehension using if-else in Python:

List Comprehension Using if Statement

In this program, we have used only an if statement to check whether it is an even number or not. Here, we use the below-mentioned syntax for list comprehension.

Syntax of List Comprehension With If Statement

[ variable for variable in sequence if(condition) ]

In the program, we have created a list using the list comprehension. This program prints the even numbers between the range inclusively between 1 and 10. The List comprehension is used in the program instead of writing the code in multiple statements with the if clause Python.

Python3




list1 = [ i for i in range(1,11) if i%2==0 ]
print("Using only if statement ")
print("Printing the even numbers in the list")
print(list1)


Output

Using only if statement 
Printing the even numbers in the list
[2, 4, 6, 8, 10]


List Comprehension Using if-else

In this example, a list comprehension (`list1`) is used to create a list that contains the elements from the range 1 to 10. If the element is even (divisible by 2), it is included in the list; otherwise, it is replaced with 0. The resulting list is then printed.

Python3




list1 = [i if i % 2 == 0 else 0 for i in range(1, 11)]
 
print("The List is ")
print(list1)


Output

The List is 
[0, 2, 0, 4, 0, 6, 0, 8, 0, 10]


List Comprehension Using Multiple if-else

In this example, a list comprehension (`list1`) is created with conditional expressions. It assigns “Not char” if the character is not alphabetic, “Harsha” if the character is “H”, and “Krishna” otherwise, iterating over the characters in the string “H/K”. The resulting list is then printed.

Python3




list1 = ["Not char" if not i.isalpha() else "Harsha" if i ==
         "H" else "Krishna" for i in "H/K"]
 
print("Using the multiple if/else in  List comprehension")
 
print(list1)


Output

Using the multiple if/else in  List comprehension
['Harsha', 'Not char', 'Krishna']


Nested List Comprehension Using if-else

In this example, a nested list comprehension (`list_comp`) is used. It creates a list of lists based on the characters in the string “G1”. If the character is alphabetic, it includes the elements from the `firstList` (“Harsha”, “Krishna”, “Gowtham”, “Vasanth Kumar”, “Raghav”). If the character is not alphabetic, it includes numbers from 1 to 5. The result is then printed using a loop.

Python3




string = "G1"
firstList = ["Harsha", "Krishna", "Gowtham", "Vasanth Kumar", "Raghav"]
 
list_comp = [[k for k in firstList] if i.isalpha(
) else [j for j in range(1, 6)] for i in string]
 
print("Using the nested comprehension in the Python along with the if /else ")
for i in list_comp:
    print(i)


Output

Using the nested comprehension in the Python along with the if /else 
['Harsha', 'Krishna', 'Gowtham', 'Vasanth Kumar', 'Raghav']
[1, 2, 3, 4, 5]




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads