Python List Comprehension using If-else condition

In this Python tutorial, we will explore how to use List Comprehension together with if-else condition statements.

Normally List comprehension is used for the initialization of lists. By adding an if-statement to our list comprehension, we get to add a “filter” or “condition” on whether a value gets added or not. For e.g: Only initializing a list with even numbers.


Using List Comprehension with a condition

If we use regular list comprehension, we get the following results when initializing a list with values from 1 to 10.

mylist = [i for i in range(1, 10)]
print(mylist)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Now let’s try adding a condition here to exclude all odd values, and only include the even values in our new list.

The below code will only add the value i if it is even.

mylist = [i for i in range(1, 10) if i % 2 == 0]
print(mylist)
[2, 4, 6, 8]

Removing all even values

Now let’s try the same thing, but this time we will only initialize our list with odd values.

mylist = [i for i in range(1, 10) 
                        if i % 2 != 0]
print(mylist)
[1, 3, 5, 7, 9]

As shown in this example, you can move the if conditional statement to a new line to improve readability.


Removing all non-zero values

Here is not example where we are filtering out the 0’s from a pre-exiting list of values.

data = [3, 0, 6, 4, 0, 1, 2, 0, 9, 7, 0, 8]

mylist = [i for i in data if i != 0]
print(mylist)

[3, 6, 4, 1, 2, 9, 7, 8]

Initialize with only Strings

Another practical use is the filtering out values of a certain data-type from a List, which can contain values of varying data-types.

data = [3, "0", 6, 4, "0", 1, "2", 0, "9", 7, 0, "8"]

mylist = [i for i in data if isinstance(i, int)]
print(mylist)
[3, 6, 4, 1, 0, 7, 0]

Using “else” in a List comprehension

Using “else” in a List comprehension requires you to change the structure a bit. Although it looks similar, it’s not the same thing as what we were doing earlier, so don’t confuse the two.

In this, we aren’t adding a “condition”, rather we are creating two possible ways of adding a value to our list. So in both cases, we will be adding a value to the list, but we may be modifying them in a certain manner in one or more of these paths.

For example, let’s say we have a list of integers and strings, and we want to increment them all by one. We cannot increment the strings directly, and must convert them to integers first. So here we will use an if-else statement.

data = [3, "0", 6, 4, "0", 1, "2", 0, "9", 7, 0, "8"]

mylist = [int(i)+1 if isinstance(i, str) else i+1 for i in data]
print(mylist)

In the above example, the regular integers are incremented normally by one. The string values however, are first converted to integers and then incremented by one. Hence the output is also in all integers.

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

This marks the end of the Python List Comprehension using If-else condition Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments