Computer >> Computer tutorials >  >> Programming >> Python

Python program to print even numbers in a list


In this article, we will learn about the solution and approach to solve the given problem statement.

Problem statement

Given a list iterable, we need to print all the even numbers in the list.

Here we will be discussing three approaches for the given problem statement.

Approach 1 − Using enhanced for loop

Example

list1 = [11,23,45,23,64,22,11,24]
# iteration
for num in list1:
   # check
   if num % 2 == 0:
      print(num, end = " ")

Output

64 22 24

Approach 2 − Using filter & lambda function

Example

list1 = [11,23,45,23,64,22,11,24]
# lambda exp.
even_no = list(filter(lambda x: (x % 2 == 0), list1))
print("Even numbers in the list: ", even_no)

Output

Even numbers : [64, 22, 24]

Approach 3 − Using list comprehension

Example

list1 = [11,23,45,23,64,22,11,24]
#list comprehension
even_nos = [num for num in list1 if num % 2 == 0]
print("Even numbers : ", even_nos)

Output

Even numbers : [64, 22, 24]

Conclusion

In this article, we learned about the approach to print even numbers in the input list.