Filter Even Values from a List - Python
Last Updated :
04 Feb, 2025
The task of filtering even values from a list in Python involves selecting elements from the list that are even numbers. For example, given the list a = [1, 2, 3, 4, 5], the goal is to filter out the even values, resulting in a new list like this [2, 4].
Using list comprehension
List comprehension provides a efficient way to filter values from a list. It is often the most Pythonic and recommended approach for filtering, as it is both readable and fast. The syntax is compact and the operation is performed in a single line.
Python
a = [1, 2, 3, 4, 5]
res = [num for num in a if num % 2 == 0]
print(res)
Explanation: list comprehension iterates over each element in a and includes it in res only if it's even (num % 2 == 0).
Using filter
filter() allows us to filter elements from an iterable based on a function. Combined with a lambda function, it offers a very compact and functional programming approach to filtering even values.
Python
a = [1, 2, 3, 4, 5]
res = list(filter(lambda num: num % 2 == 0, a))
print(res)
Explanation: filter() applies the lambda function to each element in a. The lambda num: num % 2 == 0 checks if a number is even and filter() returns an iterator with only the even numbers, which is then converted to a list using list().
Using for loop
For loop is the traditional and manual approach to iterate over a list and filter values. While it works perfectly fine, it is not as efficient or compact as the other methods. We manually check each element and append the even values to a new list.
Python
a = [1, 2, 3, 4, 5]
res = []
for num in a:
if num % 2 == 0:
res.append(num)
print(res)
Explanation: For loop iterates over each element in the list a. For each element, it checks if the number is even using the condition num % 2 == 0. If the condition is true, the number is added to the res list using append() .
Similar Reads
Remove falsy values from a list in Python Removing falsy values from a list filters out unwanted values like None, False, 0 and empty strings, leaving only truthy values. It's useful for data cleaning and validation.Using List ComprehensionList comprehension is a fast and efficient way to remove falsy values from a list . It filters out val
1 min read
Remove empty tuples from a list - Python The task of removing empty tuples from a list in Python involves filtering out tuples that contain no elements i.e empty. For example, given a list like [(1, 2), (), (3, 4), (), (5,)], the goal is to remove the empty tuples () and return a new list containing only non-empty tuples: [(1, 2), (3, 4),
3 min read
Filter List of Dictionaries Based on Key Values in Python Filtering a list of dictionaries based on specific key values is a common task in Python. This becomes useful when working with large datasets or when we need to select certain entries that meet specific conditions. We can do this using list comprehensions, the filter() function, and simple loops.Us
3 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
How to Skip a Value in a List in Python Skipping a value in a list means we want to exclude certain items while iterating. There are many ways to skip a value in Python List. Using continue is the simplest way to skip a value in iteration, without modifying the original list. Pythona = [1, 2, 3, 4, 5] # iterating the list for i in a: if i
2 min read