Even number is an integer number that is divisible by 2 and leaves 0 reminder.
In this article, we will learn how to print only even numbers of a list in Python. To find even numbers, we can use several approaches such as using loops, list comprehension, lambda expression, etc.
Let's get started with some Python program examples.
In this approach, we will first loop through the whole list. For each element, we check if it is divisible by 2 or not. If a number is divisible by two then it is an even number and we can print it. Let us understand this approach with an algorithm.
Initialize a list je_list
Apply a for loop for the list je_list
For each element, je_ele check if divisible by 2
If yes, then print je_ele
Else continue
# Initialise a list
je_list = [ 1, 3, 43, 66, 321, 4 ]
​
# apply a loop to the list
for je_ele in je_list:
 # check for divisiblity by 2
 if je_ele % 2 == 0:
   # if divisible by 2 then print
   print(je_ele)
66
4
This approach is similar to the previous approach except that here we use the while loop instead of the for loop.
Here, we use a looping variable and initialize it 0 before the loop. The condition for the while loop is that the looping variable should be less than the length of the list. Then inside the loop, we check if the value in the list at the looping variable's value is divisible by 2. if the number is divisible by 2 then it is an even number and, we can print it. Let us understand this approach with an algorithm.
Initialize a list je_list
Declare a looping variable je_index and initialize it to 0
While je_index < len(je_list) do
check if divisible je_list[je_index] by 2
If yes, then print je_list[je_index]
else do nothing
Increment je_index
# Initialise a list
je_list = [ 1, 3, 43, 66, 321, 4 ]
# declare and initialize a looping variable
je_index = 0
​
while je_index < len(je_list):
  # check for divisiblity by 2
  if je_list[je_index] % 2 == 0:
    # if divisible by 2 then print
    print(je_list[je_index])
  # Increment the looping variable
  je_index = je_index + 1
Output:
66
4
Python gives us a concise way of creating a new list based on the values of an existing list called list comprehension.
In this approach, we use List comprehension to create a new list containing only the elements divisible by two. Let us understand this approach better using an algorithm.
Initialize a list je_list
Filter je_list using list comprehension and store the new list in je_newList
display list je_newList
# Initialise a list
je_list = [ 1, 3, 43, 66, 321, 4 ]
​
# create a new list using list comprehension
je_newList = [je_ele for je_ele in je_list if je_ele % 2 ==0]
​
## display the newly created list
print(je_newList)
Output:
[66, 4]
In this approach, we make use of the filter function. This function takes two arguments: a function and a list. The function is called with the list passed and a reduced result is produced. Filter means that the list is filtered depending upon a condition. In this case, we pass a lambda function as the argument.
# Initialise a list
je_list = [ 1, 3, 43, 66, 321, 4 ]
​
# create a new list using filter and lambda function
je_even = list(filter(lambda x: (x % 2 == 0), je_list))
​
## display the newly created list
print(je_even)
Output:
[66, 4]