Extract even numbers from a list in Python

In this article, we will discuss different ways to select even numbers from a list in Python.

Table Of Contents

Introduction

Suppose we have a list, that contains only numbers. Like this,

[10, 11, 11, 23, 14, 36, 12, 20, 18, 171, 78, 91, 9, 10]

We want to fetch only even numbers from this list. Like this,

[10, 14, 36, 12, 20, 18, 78, 10]

There are different ways to do this. Let’s discuss them one by one.

Advertisements

Method 1: Using List Comprehension

To fetch even numbers from a list, iterate over all numbers of the list using List comprehension, and select only even numbers. This selection can be done by using an if condition inside the List comprehension. To check if a given number is even or not, divide it by 2, if the reminder is 0, then it means that the given number is even. Let’s see an example,

# List of Numbers
listOfNumbers = [10, 11, 11, 23, 14, 36, 12, 20, 18, 171, 78, 91, 9, 10]

# Select only even numbers from a list in Python
evenNumbers = [elem for elem in listOfNumbers if elem % 2 == 0]

print(evenNumbers)

Output:

Read More  Add elements to the end of Array in Python
[10, 14, 36, 12, 20, 18, 78, 10]

We fetched only even numbers from the list.

Method 2: Using List Comprehension & AND operator

This solution is similar to previous one. But with just one difference. Although, we will iterate over all numbers of list, and select only even numbers using List Comprehension. But we will use different logic, to check if a number is even or not. Apply an AND operation between a number and digit 1, if it returns 0, then it means the given number is even. Let’s see an example,

# List of Numbers
listOfNumbers = [10, 11, 11, 23, 14, 36, 12, 20, 18, 171, 78, 91, 9, 10]

# Select only even numbers from a list in Python
evenNumbers = [elem for elem in listOfNumbers if (elem & 1) == 0]

print(evenNumbers)

Output:

Read More  Convert a Pandas Series or Index to a NumPy array
[10, 14, 36, 12, 20, 18, 78, 10]

We fetched only even numbers from the list.

Method 3: Using For Loop

This solution is similar to previous one. But with just one difference. We will use direct for loop, instead of List Comprehension. Iterate over all numbers in list using a for loop, and select even numbers only. To check if a given number is even or not, divide it by 2. If the reminder is 0, then it means the given number is even. Let’s see an example,

# List of Numbers
listOfNumbers = [10, 11, 11, 23, 14, 36, 12, 20, 18, 171, 78, 91, 9, 10]

# An empty list to hold even numbers
evenNumbers = []

# Iterate over all elements of list
for elem in listOfNumbers:
    # Check if given element/number is even
    if elem % 2 == 0:
        # If number is even then add in new list
        evenNumbers.append(elem)

print(evenNumbers)

Output:

Read More  Select Rows by Index List in Pandas
[10, 14, 36, 12, 20, 18, 78, 10]

We fetched only even numbers from the list.

Summary

We learned about three different ways to select only even numbers from a list in Python. Thanks.

Leave a Comment

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.

Scroll to Top