In this tutorial, we will learn about python list comprehension and how to use if else in list comprehension with example code.

What is Python List Comprehension?

Python List Comprehension gives us a shorter syntax while creating a new list based on the values of an existing one. It’s an easier way to create a list from a string or another list.

It is faster than processing a list using the for loop.

Moreover, if statements and for loops are powerful ways to write clean, well-organized and logical code in Python.

For example,

list=[]
for i in 'anxiety':
    mylist.append(i)
print(list)

As a result, the output is:
[‘a’, ‘n’, ‘x’, ‘i’, ‘e’, ‘t’, ‘y’]

Lambda vs Python List Comprehension

Python Lambda expression returns an expression’s value which it calculates using values of the arguments it gets. First, we create a list from a set in list comprehension syntax:

myset={2,1,3}
makelist=lambda i:list(i)
mylist=makelist(myset)
print(mylist)

As a result, the output is:
[1, 2, 3]

Here, we can clearly see that the set rearranges itself as {1,2,3}. Then, we created a lambda function storing it in the variable ‘makelist’. The lambda function takes a value converting it into a list and then returns it. After that, we called makelist on myset, and stored it in the variable mylist which now holds a list.

But we can do the same thing using the map function. It can be done using the following code:

list(map(lambda i:i,myset))

As a result, the output will be:
[1, 2, 3]

Here, we take a lambda function, for each value of “i” it returns “i “and maps this on each value in myset. Then it converts this into a python list and prints it.

Note:- Python list comprehension’s biggest advantage over lambda function is that it’s more readable.

Python list comprehension with if statement

In this example, we perform list comprehension using the if statement in Python.

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

As a result, the output is:
[0, 2, 4, 6, 8]

Python list comprehension with if statement(without else)

In this example, we perform list comprehension using if statement(but without else) in Python.

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

As a result, the output is:
[5, 6, 7, 8, 9]

Here, we have taken a variable as num, the num = [i for i in range(10) if i>=5] is used for iteration. Then we have used for loop and assigned a range of 10, and then if condition is used as if >= 5. Then we use the print command to get the output.

Python list comprehension with Nested if statement

In this example, we perform list comprehension using the Nested if statement in Python.

num_list = [y for y in range(100) if y % 2 == 0 if y % 5 == 0]
print(num_list)

As a result, the output is:
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]

Here, the list comprehension checks if y is divisible by 2 and if y is divisible by 5. If y satisfies both conditions, it is appended to num_list.

If else list comprehension Python

In this example, we perform list comprehension using the if-else statement in Python.

obj = ["Even" if i%2==0 else "Odd" for i in range(10)]
print(obj)

As a result, the output is:
['Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd']

Here, the list comprehension will check 10 numbers from 0 to 9. If i is divisible by 2, then “Even” is appended to the obj list. If not, then “Odd” is appended.

2D list Comprehension Python with Nested for loop statement

Here, we perform nested iteration inside a list comprehension. We find out the transpose of a matrix using a nested loop inside list comprehension.

matrix = [[1, 2], [3,4], [5,6], [7,8]]
transpose = [[row[i] for row in matrix] for i in range(2)]
print(transpose)

As a result, the output is:
[[1, 3, 5, 7], [2, 4, 6, 8]]

In the above example, we have a variable matrix that has 4 rows and 2 columns. We need to find the transpose of the matrix. For that purpose, we used list comprehension.

Conclusion

List Comprehension is a process to create new lists from existing lists, after applying transformations or changes to them. It is more compact and faster.

Moreover, we should not write long list comprehensions in one line, so that it remains user-friendly and readable. Also, we can write a for-loop for every list comprehension in python, but we can’t write list comprehensions for complex for-loops.

You can also learn about Get Key from Value Python

Like this article? Follow us on Facebook and LinkedIn.