Python program to print even numbers in a list


Python Programming Language is one of the most efficient and user-friendly programming language and have endless uses and applications. Lists declared in Python are analogous to dynamically sized arrays in other programming languages (vector in C++ and ArrayList in Java). A list is simply a collection of items enclosed by [] and separated by commas.

In this tutorial, we will learn about the solution and approach to find out all even numbers in a given list using Python. List is one of the most fundamental data structures in python. They are widely used and they store similar contiguous data. A number is considered even if it divides by 2 evenly, i.e., leaving no remainder.

We will go through three methods to find all even numbers in a list.

  • Using modulo operator

  • Using Bitwise and Operator

  • Checking last digit of the number

Using Modulo Operator

Modulo operator (%) returns the remainder when the first argument is divided by the second one.

Examples

  • 6 % 4 = 2

  • 15 % 4 = 3

  • 27 % 6 = 3

  • 30 % 8 = 6

For a number to be even, its remainder upon dividing by 2 should be 0.

Mathematically, if x % 2 == 0, then x is said to be even.

We can check whether this condition holds for each element in the list and print theoutput.

This approach takes O(N) time, where N is the size of range.

Syntax

x = 7
result = (x % 2 == 0)

Example

In the example below, we have implemented the above approach. We have created a function to filter out all the even numbers in the given list.

def evens(nums_list): # iteration for num in nums_list: # check for remainder if num % 2 == 0: print(num, end=' ') evens([5, 20, 21, 58, 3])

Output

20 58

Using Bitwise and Operator

In this approach we will use the Bitwise And (&) operator. A computer understands binary natively. Any whole number can be represented as bits (0’s and 1’s), which basically specify which power of 2 we should include and which should be excluded.

Example: 1011 means, going from right to left, 1*(2^0) + 1*(2^1) + 0*(2^2) + 1*(2^3) = 11

Bitwise And operator performs & operation on each bit. This operator returns 1 if both bits are set and 0 otherwise. Example: 0110 & 1010 = 0010

Apart from 2^0-th bit, or the rightmost bit, all other bits are powers of 2, which means summing them will always give an even number. So we can simply check whether the last bit is set or not to determine whether the number is odd or even.

This approach also takes O(N) time, where N is the size of range.

Syntax

x = 7
result = (x & 1 == 0)

Example

In the below example we perform bitwise & with 1 on each number. It will check whether the last bit is set or not. If it is unset that means the number is even.

def evens(nums_list): # iteration for num in nums_list: # check for remainder if num & 1 == 0: print(num, end=' ') evens([5, 20, 21, 58, 3])

Output

20 58

By checking the last digit of the number

By sheer observation we can identify that if a number is even, it must end with 0, 2, 4, 6, or 8. This can be used in along with modulo with 10, as mod 10 gives the last digit of a number.

This approach takes O(N * 5) time, as each element is checked if present in a tuple of size 5.

Examples

  • 12345 % 10 = 5 (odd)

  • 4232 % 10 = 2 (even)

Syntax

x = 7
result = x % 10 in (0, 2, 4, 6, 8)

Example

We use ‘in’ keyword here for a quick look-up in the tuple. A set can also be used which provides constant loop-up time.

def evens(nums_list): # iteration for num in nums_list: # check for remainder if num % 10 in (0, 2, 4, 6, 8): print(num, end=' ') evens([5, 20, 21, 58, 3])

Output

20 58

A little Python Bonus

Python contains a built-in function called filter(), which returns an iterator.

It takes two arguments, first is a Boolean function, second is the iterable on which it should be applied. We can use any of the above methods as a function for this.

This approach also takes O(N) time, where N is the size of range.

Special care must be taken where using filter() as it is exhaustible, meaning once it is used, either for traversal or for conversion to list, it will return None if traversed again, so it’s better to convert it to a list and store in another variable.

Syntax

evens = filter(lambda x: x % 2 == 0, [5, 20, 21, 58, 3])

Example

In the below example we perform bitwise & with 1 on each number. It will check whether the last bit is set or not. If it is unset that means the number is even.

def evens(nums_list): even_numbers = filter(lambda x: x % 2 == 0, nums_list) for num in even_numbers: print(num, end=' ') evens([5, 20, 21, 58, 3]

Python One Liner using filter()

Example

In the below example we perform bitwise & with 1 on each number. It will check whether the last bit is set or not. If it is unset that means the number is even.

def evens(nums_list): print(*filter(lambda x: x % 2 == 0, nums_list)) evens([5, 20, 21, 58, 3])

Output

20 58

Updated on: 13-Oct-2022

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements