Print even numbers in a list - Python
Last Updated :
09 May, 2025
Getting even numbers from a list in Python allows you to filter out all numbers that are divisible by 2. For example, given the list a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], you might want to extract the even numbers [2, 4, 6, 8, 10]. There are various efficient methods to extract even numbers from a list. Let's explore different methods to do this efficiently.
Using List comprehension
List comprehensions is an efficient way to filter elements from a list. They are generally considered more Pythonic and faster than traditional loops.
Python
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
res = [val for val in a if val % 2 == 0]
print(res)
Explanation: [val for val in a if val % 2 == 0] iterates through the list a and selects only the even numbers.
Using filter()
filter() function provides a functional programming approach to filter elements from a list. It can be a bit less intuitive compared to list comprehensions, but it is still very useful.
Python
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
res = list(filter(lambda val: val % 2 == 0, a))
print(res)
Explanation: filter() function takes lambda function that tests each element in the list. lambda val: val % 2 == 0 returns True for even numbers, which are then included in the final list. We convert the result to a list using list().
Using loop
Using a traditional for loop is the most straightforward method, but it is generally less efficient and more verbose compared to other methods.
Python
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for val in a:
if val % 2 == 0:
print(val, end=" ")
Explanation: For loop iterates through each element in the list a. If the element is divisible by 2 (even number), it is printed.
Using Bitwise AND operator
This is a low-level, bitwise approach to check if a number is even. It works by performing a bitwise AND operation with 1, which will return 0 for even numbers and 1 for odd numbers.
Python
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
res = [val for val in a if val & 1 == 0]
print(res)
Explanation: expression val & 1 == 0 uses the bitwise AND operator to check if the least significant bit of a number is 0. Even numbers have their least significant bit as 0, so the condition evaluates to True for even numbers.
Related Articles
Similar Reads
Print lists in Python Printing a list in Python is a common task when we need to visualize the items in the list. There are several methods to achieve this and each is suitable for different situations. In this article we explore these methods.The simplest way of printing a list is directly with the print() function:Pyth
3 min read
Python end parameter in print() In Python, the print() function, commonly used for displaying output, by default ends each statement with a newline character (\n), but this behavior can be customized using the end parameter, which allows you to specify a different string (such as a space, comma, or hyphen) to be printed at the end
3 min read
Multiply All Numbers in the List in Python Our task is Multiplying all numbers in a list Using Python. This can be useful in calculations, data analysis, and whenever we need a cumulative product. In this article we are going to explore various method to do this. Using a loopWe can simply use a loop (for loop) to iterate over the list elemen
2 min read
Access List Items in Python Accessing elements of a list is a common operation and can be done using different techniques. Below, we explore these methods in order of efficiency and their use cases. Indexing is the simplest and most direct way to access specific items in a list. Every item in a list has an index starting from
2 min read
Iterate over a list in Python Python provides several ways to iterate over list. The simplest and the most common way to iterate over a list is to use a for loop. This method allows us to access each element in the list directly.Example: Print all elements in the list one by one using for loop.Pythona = [1, 3, 5, 7, 9] # On each
3 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read