Python program to print even numbers in a list | Example code

  • by

A simple approach to print even numbers in a list is to Iterate each element in the list using for loop and check if num % 2 == 0, If the condition satisfies, then only print the number.

For that, you can use for loop or filter & lambda function or list comprehension with if statements.

Python program to print even numbers in a list

Simple example code will print all the even numbers in a list.

Using enhanced for loop

list1 = [11, 23, 45, 23, 64, 22, 11, 24]

# iteration
for num in list1:
    # check
    if num % 2 == 0:
        print(num, end=" ")

Output:

Python program to print even numbers in a list

Using filter & lambda function

list1 = [11, 23, 45, 23, 64, 22, 11, 24]

even_no = list(filter(lambda x: (x % 2 == 0), list1))

print(even_no)

Output: [64, 22, 24]

Using list comprehension

list1 = [11, 23, 45, 23, 64, 22, 11, 24]

even_nos = [num for num in list1 if num % 2 == 0]
print(even_nos)

Output: [64, 22, 24]

Using while loop

list1 = [10, 24, 4, 45, 66, 93]
num = 0

while num < len(list1):
    if list1[num] % 2 == 0:
        print(list1[num], end=" ")
    num += 1

Output: 10 24 4 66

Do comment if you have any doubts or suggestions on this Python number topic.

Note: IDE: PyCharm 2021.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.