Skip to content

Python filter: A Complete Guide to Filtering Iterables

Python filter A Complete Guide to Filtering Iterables Cover Image

The Python filter function is a built-in way of filtering an iterable, such as a list, tuple, or dictionary, to include only items that meet a condition. In this tutorial, you’ll learn how to use the filter() function to filter items that meet a condition. You’ll learn how to use the function to filter lists, tuples, and dictionaries. You’ll also learn how to streamline your filter functions with anonymous lambda functions.

By the end of this tutorial, you’ll have learned:

  • How the Python filter() function can be used to filter iterable objects, such as lists, tuples, dictionaries, and more
  • How the Python filter() function makes provides a simpler, repeatable way of filtering objects
  • How to use anonymous lambda functions to make your filtering more straightforward in Python

Understanding the Python filter Function

The Python filter() function is a built-in function that lets you pass in one iterable (such as a list) and return a new, filtered iterator. The function provides a useful, repeatable way to filter items in Python.

Let’s take a look at the syntax of how the filter function works:

# Understanding the Python filter() Function
filter(
   function,    # The function to filter items with
   iterable     # The iterable to filter
)

The way that this works is by passing in a function that returns a boolean value, which is used to filter values. Before diving into practical examples of the function, let’s take a look at how you might filter a list of items without using the filter function.

Filtering Lists in Python Without the filter Function

In this section, we’ll explore how you might filter a list of items in Python without using the filter() function.

Say that we have the following list: [1,2,3,4,5,6,7,8,9] and we want to filter the list to only include items that are larger than 5. Let’s see how we’d do this in Python using a for loop:

# Filtering a List with a For Loop
values = [1,2,3,4,5,6,7,8,9]
filtered = []

for value in values:
    if value > 5:
        filtered.append(value)

print(filtered)

# Returns: [6, 7, 8, 9]

Let’s break down what we did in the code above:

  1. We instantiated an empty list, filtered, which will be used to hold items
  2. We then looped over each value in our list, values
  3. In the loop, we assessed whether the value is greater than 5. If it is, the value is appended to the list.

While this approach works, it’s not the best implementation. Let’s explore why this is the case:

  • We first need to instantiate an empty list to hold filtered values
  • The approach only works for a single iterable at a given time, meaning that it’ll need to be re-written if you want to filter another list

Now that you have a strong understanding of how to filter lists using a for loop, let’s see how we can simplify this task with the Python filter() function.

Filtering Lists in Python With the filter Function

In the previous section, you learned how to filter a Python list using a for loop. In this section, you’ll learn how to make the process of filtering lists easier and more Pythonic using the Python filter() function.

You may recall that the filter() function takes both a function and an iterable object as its arguments. Because of this, we need to define a function that returns a boolean value based on the filter criteria we want. We can then pass this function into the filter() function.

Let’s see how we can replicate our earlier example of filtering our list to include only values greater than 5:

# Exploring how to use the Python filter() Function
values = [1,2,3,4,5,6,7,8,9]

def greater_than_five(x):
    return x > 5

filtered = filter(greater_than_five, values)
filtered_values = list(filtered)

print(filtered_values)

# Returns: [6, 7, 8, 9]

Let’s break down what we did here:

  1. We defined a function, greater_than_five(), which takes a single value as its input. The function returns a boolean value, evaluating if the value is greater than 5.
  2. We then created a new filter object by passing in the callable of the function (without the parentheses) and the list itself.
  3. We then created a list out of this filter object to return just the values.

We can see that this approach is much more intentional – it directly communicates what you’re doing. The intention of the filter() function is to filter the data. This approach was not as clear when we used a for loop.

In the following section, you’ll learn how to simplify this even further by making use of anonymous lambda functions.

Using Anonymous Lambda Functions with Python filter

In the previous section, you learned how to use the Python filter() function to filter a list. In this section, you’ll learn how to simplify this process even further by using lambda functions.

Why would you want to do? In many cases, you won’t need to use the function you’re using to filter for anything else. Because of this, using a lambda function removes a lot of the ambiguity of what the function is meant to be used for.

Let’s see how we can replicate our earlier example of filtering a list of values from 1 through 9 to only include values greater than 5:

# Using lambda Functions in the Python filter() Function
values = [1,2,3,4,5,6,7,8,9]

filtered = filter(lambda x: x>5, values)
filtered_values = list(filtered)

print(filtered_values)

# Returns: [6, 7, 8, 9]

Right off the bat, we can see how much shorter this code is. Because the lambda function is defined within the filter() function itself, the intention of the code is much clearer.

Practical Examples of the Python filter Function

In this section, we’ll explore some further practical examples of how the Python filter() function can be used to filter lists. Following this section, we’ll explore how to use the function to filter lists of dictionaries, lists of tuples, and strings.

Filtering Even Numbers in Python with the filter Function

In this example, we’ll explore filtering a list of numbers to only return even numbers. In order to do this, we can use the % modulo operator. This operator returns the remainder of a division. If the remainder between a number and 2 is 0, then the number is odd.

Let’s see how we can do this using the filter() function:

# Using the Python filter() Function to Filter a List to Only Even Numbers
values = [1,2,3,4,5,6,7,8,9]

filtered = list(filter(lambda x: x % 2 == 0, values))
print(filtered)

# Returns: [2, 4, 6, 8]

Filtering Words Longer than n Characters in a Python List

In this example, you’ll learn how to filter a list of strings to only include strings that are longer than a given length. Let’s see how we can do this:

# Filtering a List of Strings
strings = ['hello', 'world', 'yes', 'no', 'maybe']

filtered = list(filter(lambda x: len(x) > 4, strings))
print(filtered)

# Returns:
# ['hello', 'world', 'maybe']

Filtering a List of Dictionaries with Python filter

In this section, you’ll learn how to use the Python filter() function to filter a list of dictionaries. When working with web data, it’s common to get data in the JSON format, which you can easily convert to lists of Python dictionaries.

In a list of dictionaries, when we iterate over each item, we’re iterating over each dictionary. Because of this, we can access the value of a given key by accessing that key. We can then evaluate if the key meets a condition.

Let’s take a look at an example:

# Filtering a List of Dictionaries with the Python filter() Function
ages = [
    {'name': 'Evan', 'age': 25},
    {'name': 'John', 'age': 30},
    {'name': 'Jane', 'age': 27},
    {'name': 'Jack', 'age': 32},
]

filtered = list(filter(lambda x: x['age'] >= 30, ages))
print(filtered)

# Returns:
# [{'name': 'John', 'age': 30}, {'name': 'Jack', 'age': 32}]

Let’s break down what we did here:

  1. We generated a list of dictionaries containing ages and names
  2. We then filtered the list using a lambda function that accesses the 'age' key and checks if the value is greater than or equal to 30

Filtering a List of Tuples with Python filter

In this final section, you’ll learn how to filter a list of tuples using the Python filter() function. In many cases, database queries will return a list of tuples. Being able to filter these objects is an important skill.

Because tuples themselves are ordered, we can access a given index and evaluate if it meets a condition. In the example below, we have a list of tuples that contain dates and their respective sale amounts. We can filter our list to only tuples where the sale amount is greater or equal to 150.

# Filtering a List of Tuples with filter()
sales = [
    ('2022-06-01', 150),
    ('2022-06-02', 100),
    ('2022-06-03', 125),
    ('2022-06-04', 75),
    ('2022-06-05', 155),
    ('2022-06-06', 180)
]

filtered = list(filter(lambda x: x[1] >= 150, sales))
print(filtered)

# Returns:
# [('2022-06-01', 150), ('2022-06-05', 155), ('2022-06-06', 180)]

Conclusion

In this post, you learned how to use the Python filter() function to filter iterable objects, such as lists. You first learned how to filter lists without using the filter() function. Then, you learned how using the Python filter() improves upon this. Then, you learned how to filter iterables using lambda functions. Finally, you explored examples of filtering lists, lists of dictionaries, and lists of tuples.

Additional Resources

To learn more about related topics, check out the tutorials below:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

2 thoughts on “Python filter: A Complete Guide to Filtering Iterables”

  1. filter() can also be used to remove None-values from a list:

    list(filter(None, [1, 2, None, ‘a’, 7.5, None]))
    [1, 2, ‘a’, 7.5]

Leave a Reply

Your email address will not be published. Required fields are marked *