Blog Post #131: Functional Programming 102: The filter() Function

In Post #130, we explored the map() function, a tool for applying a transformation to every item in a sequence. But what if we don’t want to transform the items, but rather select a subset of them that meets a certain condition?

For this task, Python provides another core functional tool: the filter() function. In this post, you’ll learn how to use filter() to create a new sequence containing only the elements that pass a test you define.

What Does filter() Do?

The filter() function takes two arguments: a function and an iterable (like a list).

The syntax is: filter(function, iterable)

The provided function must be one that returns a boolean value (True or False). filter() applies this “test” function to every item in the iterable.

Like map(), filter() is “lazy.” It returns a special filter object, which is an iterator. This iterator will only yield the items from the original iterable for which the test function returned True. To see all the results at once, we typically convert this iterator into a list using list().

filter() in Action

Using filter() with a def Function

Let’s start with an example where we want to filter a list of numbers to get only the even ones. First, we need a function that can test if a number is even.

def is_even(number):
    """Returns True if a number is even, False otherwise."""
    return number % 2 == 0

numbers = [1, 2, 3, 4, 5, 6, 7, 8]

# The filter() function will apply is_even to each number
even_numbers_iterator = filter(is_even, numbers)

# Convert the iterator to a list to see the results
even_numbers = list(even_numbers_iterator)

print(even_numbers)

The output will be a new list containing only the numbers that passed the is_even test: [2, 4, 6, 8].

Using filter() with a lambda Function

As we saw with map(), a lambda function is perfect for a simple, one-line test like this. We can achieve the same result more concisely without defining a separate is_even function.

numbers = [1, 2, 3, 4, 5, 6, 7, 8]

# The lambda x: x % 2 == 0 provides the test directly
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))

print(even_numbers)

This is the most common and “Pythonic” way you’ll see filter() used in practice.

map() vs. filter(): A Quick Review

It’s easy to confuse these two functions. Here’s a simple way to remember the difference:

  • map() is for transforming: It takes in a sequence and returns a new sequence of the same length, where each item has been modified.(e.g., [1, 2, 3] -> map -> [1, 4, 9])
  • filter() is for selecting: It takes in a sequence and returns a new sequence of the same or shorter length, containing only the original items that passed a test.(e.g., [1, 2, 3, 4] -> filter -> [2, 4])

What’s Next?

The filter() function is a powerful and memory-efficient tool for selecting items from a sequence based on a condition. Paired with a lambda, it provides a concise, functional way to extract the data you need.

You might have noticed that the things we’re doing with map() and filter() look very similar to what we can do with list comprehensions. Which approach is more “Pythonic”? And which should you use? In Post #132, we will compare map and filter directly with comprehensions.

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment