Javaexercise.com

Python Program To Find Even Numbers In A List

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.

Find even number in a list using for loop in Python

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.

Algorithm to find even number in Python list

  1. Initialize a list je_list

  2. Apply a for loop for the list je_list

  3. For each element, je_ele check if divisible by 2

  4. If yes, then print je_ele

  5. Else continue

Python Code:

# 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)

Output:

66

4

Find even number in a list using while loop in Python

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.

Algorithm

  1. Initialize a list je_list

  2. Declare a looping variable je_index and initialize it to 0

  3. While je_index < len(je_list) do

  4. check if divisible je_list[je_index] by 2

  5. If yes, then print je_list[je_index]

  6. else do nothing

  7. Increment je_index

Python Code

# 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

Find even number in a list using list comprehension in Python

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.

Algorithm

  1. Initialize a list je_list

  2. Filter je_list using list comprehension and store the new list in je_newList

  3. display list je_newList

Python Code:

# 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]

Find even number in a list using lambda expressions in Python

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]