Published on

Python filter() Function

Authors

Introduction

The filter() function is a built-in function in Python that's used to filter values in an iterable - for example, a list or dictionary - and then returning a new iterator containing elements from the original iterable that satisfy the condition specified in the filtering function.

In this article, we'll explore the filter() function, discover its capabilities and show you examples of how you can use it for data manipulation.

filter() - Syntax and Examples

Syntax

The filter() function has the following signature:

filter(function, iterable)
  • function: The function that contains the condition to filter the iterable. It could be a normal python defined with def, or a lambda function.
  • iterable: The iterable on which to perform the filtering.

Examples

Consider the following example which uses the regular def keyword to create a filter function that checks for the prime numbers in a list:

def is_prime(num):
    if num <= 1:
        return False
    for i in range(2, int(num**0.5) + 1):
        if num % i == 0:
            return False
    return True

# Generate a list of numbers from 1 to 20
numbers = list(range(1, 21))


prime_numbers = list(filter(is_prime, numbers))
print(prime_numbers)

In this example, the filter function works by running the is_prime function in each of the element in the numbers list. The is_prime function returns True if the number num is prime and False otherwise. We then use this function as the filtering condition in the filter() function to extract the prime numbers from the numbers list, resulting in a new list containing only the prime elements. Finally, we use the listfunction to convert the results from the filter function into a list.

Here's another example that uses an anonymous lambda function to apply a filter to a list of numbers to check for all the even numbers in the list.

numbers = list(range(1, 16))
is_even = list(filter(lambda x: x % 2 == 0, numbers))
print(is_even)

Running the code above prints the following output to stdout:

[2, 4, 6, 8, 10, 12, 14]

Using None with filter()

Passing None as the first argument to filter() is equivalent to the following expression: item for item in iterable if item. This is especially useful for filtering out falsy values, such as an empty string, empty list or an empty dictionary in an iterable.

Consider the following example which filters a list and returns only the non-falsy values:

data = [0, 1, '', None, False, 'hello', [], {}, 42, 'world', (), True]
non_falsy_values = list(filter(None, data))
print(non_falsy_values)

In the above example, we're passing None as the filtering function which checks the data list for any value that is equivalent to false in Python.

Running the above code returns the following result:

[1, 'hello', 42, 'world', True]

Chaining multiple filter() operations

Since the return value of a filter() function is an iterable, it's possible to chain multiple filter()functions.

Let's look at the following example which checks for even numbers in a list and then run another check for even numbers greater than four:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
filtered_numbers = list(
    filter(lambda x: x % 2 == 0, filter(lambda x: x > 4, numbers))
)
print(filtered_numbers)

Running the code above gives us the following output:

[6, 8, 10]

filter() vs. List Comprehension

A common practice you'll often find in the Python world is the use of list comprehension (and dictionary comprehension) in place of the filter()function to filter out elements from an iterable based on certain conditions.

While both tools are useful for data manipulation, there are cases where you'll find one preferable over the other.

For example, if you're working with a large dataset using the built-in filter function might be more appropriate because it's more efficient and separates the filtering logic from the data manipulation.

List comprehension, on the other hand, uses a compact syntax that feels more concise and Pythonic and is often a go-to choice for simple filtering and data manipulations.

Consider the last example that uses multiple chaining of the filter function rewritten in just a few lines of code:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
filtered_numbers = [x for x in numbers if x % 2 == 0 and x > 4]
print(filtered_numbers)

Conclusion

In this article, we explored the built-in filter() function in Python and also different examples of how you can use is for data manipulation.