Python program to print even numbers in a list

Python lists are fundamental data structures that store collections of items. Finding even numbers in a list is a common programming task with multiple efficient approaches. A number is even if it divides by 2 with no remainder.

We will explore several methods to print even numbers from a list, each with different advantages ?

Using Modulo Operator

The modulo operator (%) returns the remainder when dividing two numbers. For even numbers, x % 2 == 0 will be true.

Example

def print_evens(numbers):
    for num in numbers:
        if num % 2 == 0:
            print(num, end=' ')

print_evens([5, 20, 21, 58, 3])
20 58 

Using Bitwise AND Operator

In binary representation, even numbers have their least significant bit (rightmost) as 0. Using num & 1 == 0 checks if the last bit is unset ?

Example

def print_evens_bitwise(numbers):
    for num in numbers:
        if num & 1 == 0:
            print(num, end=' ')

print_evens_bitwise([5, 20, 21, 58, 3])
20 58 

Checking Last Digit

Even numbers always end with 0, 2, 4, 6, or 8. We can check if num % 10 is in this set ?

Example

def print_evens_last_digit(numbers):
    for num in numbers:
        if num % 10 in (0, 2, 4, 6, 8):
            print(num, end=' ')

print_evens_last_digit([5, 20, 21, 58, 3])
20 58 

Using filter() Function

Python's built-in filter() function creates an iterator of elements that satisfy a condition ?

Example

def print_evens_filter(numbers):
    even_numbers = filter(lambda x: x % 2 == 0, numbers)
    for num in even_numbers:
        print(num, end=' ')

print_evens_filter([5, 20, 21, 58, 3])
20 58 

One-liner with filter()

Using the unpacking operator (*) with filter() provides the most concise solution ?

Example

def print_evens_oneliner(numbers):
    print(*filter(lambda x: x % 2 == 0, numbers))

print_evens_oneliner([5, 20, 21, 58, 3])
20 58

Comparison

Method Time Complexity Best For
Modulo (%) O(N) Most readable and common
Bitwise (&) O(N) Fastest execution
Last digit check O(N) Educational purposes
filter() O(N) Functional programming style

Conclusion

The modulo operator method is most commonly used due to its clarity. The bitwise approach offers the best performance, while filter() provides elegant functional programming solutions.

Updated on: 2026-03-25T06:37:17+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements