How to print even numbers in a list using list comprehension with Python?

What we’ll do:

Santhosh Sudhaan
2 min readJun 10, 2021

Welcome to this blog, here in this blog we will write a program to ind area of circle with Python.

What is List comprehension?

List comprehension is one of the most most distinctive feature in Python , which you can use to create powerful functionality within a single line of code . It is an elegant way to define and create lists based on existing lists. Essentially, it is Python’s way of implementing a well-known notation for sets as used by mathematicians.

Syntax:

[expr for element in list if condition]

Program Example:

Input: list1 = [2,5,8,3,10]
Output: [2, 8, 10]

Input: list2 = [11,22,33,44,55]
Output: [22,44]

What’s happening:

First of all in our case, we declare the list with odd and even numbers in it. Next we start writing our list comprehension, that is we are saying generate me a list for every num variable in the list with condition “num % 2 == 0” being satisfied.

Now at last in the resulted list, we only have the filtered values, that is we have only even numbers from the list.

Finally we are printing the resulted list.

Source Code:

#List of numbers
list1 = [10, 21, 4, 45, 66, 93]
list2 = [11, 22, 33, 44, 55]
#Using List comprehension
even_nos1 = [num for num in list1 if num % 2 == 0]
even_nos2 = [num1 for num1 in list2 if num % 2 == 0]
#Printing resulted list
print("Even numbers in the list: ", even_nos1)
print("Even numbers in the list: ", even_nos2)

Output:

Even numbers in the list:  [10, 4, 66]
Even numbers in the list: [22, 44]

What we learnt:

Through this blog, you learnt how to print even numbers in a list using list comprehension with Python.

Thank you.

--

--