Multiple If conditions in a list comprehension in Python

Here in this tutorial, you’ll learn about multiple if conditions in Python list comprehensions.

Multiple If conditions in a list

Here you first need to understand lists in Python.

  • Lists are used to store various elements in one variable. A list is a form of variable data type. Add or remove data as needed. A list is one of the built-in data types for storing various variables. Examples: integers, floats, strings.
    new_list = [16, 'Georgia', 'Australia', 154.63 ]

A list is an ordered, resizable collection. You can also duplicate the data in the list. Sometimes you want to use a list comprehension on two lists at once.

A list comprehension provides a short syntax for creating a new list based on the values ​​of an existing list. First, let’s look at different ways to generate new lists using list comprehensions. Now let’s see the benefits of using list comprehensions. Finally, we’ll talk about how to make sense of multiple lists.

The basic if else condition in the list comprehension in the Python example is,

Stationary = ["Pen", "Register", "Copy", "Pages"]
new_list = [y for y in Stationary if "e" in y]


['Pen', 'Register', 'Pages']

From this above example, we use this for multiple if conditions in a list comprehension in Python. Here we print the if condition where the numbers in the specified range are all multiples in list form.

  • Multiple if conditions in a list comprehension in Python Example :
num_list = [i for i in range(100) if i>=5 if i<=95 if i%4==0 if i%8==0]


[8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88]

For more details, use the link below to learn more about the built-in Python list module.

Leave a Reply

Your email address will not be published. Required fields are marked *