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:

[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:

[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:

[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.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

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