Last modified: Feb 02, 2026 By Alexander Williams
Python Filter Function Guide: Filter Lists & Iterables
Python is full of tools to make your code clean and efficient. One such tool is the filter() function. It helps you sift through data quickly.
This guide explains what filter() is and how to use it. You will learn through simple examples. Let's start filtering.
What is the Python Filter Function?
The filter() function is a built-in Python tool. It processes an iterable, like a list or tuple. It applies a test function to each item.
Items that pass the test are kept. Items that fail are removed. The result is a new iterator with only the "filtered" items.
This is a core concept in functional programming. It allows you to write concise, readable data processing code.
Understanding the Filter Syntax
The syntax for the filter() function is straightforward. It takes two arguments.
# Syntax of the filter() function
filter(function, iterable)
The first argument is a function. This function defines the filtering condition. It must return either True or False for each item.
The second argument is an iterable. This is the collection you want to filter, like a list.
The function returns a filter object. This is a special iterator. You can convert it to a list or tuple to see the results.
If you need a refresher on how functions are defined, check out our Python Function Syntax Guide for Beginners.
How to Use Filter: A Basic Example
Let's start with a simple task. We have a list of numbers. We want to get only the even numbers.
First, we define a function that checks if a number is even. Then we pass it to filter().
# Define a function to test if a number is even
def is_even(num):
# Returns True if the number is divisible by 2
return num % 2 == 0
# Our original list of numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Apply the filter function
filtered_result = filter(is_even, numbers)
# Convert the filter object to a list to see the output
even_numbers = list(filtered_result)
print(even_numbers)
[2, 4, 6, 8, 10]
The filter() function applied is_even to each number. It kept only those where the function returned True.
Remember, the filter object itself is an iterator. You must convert it to a list to print or use the results directly.
Using Lambda Functions with Filter
Defining a separate function can be verbose. For simple conditions, use a lambda function. A lambda is a small, anonymous function.
This makes your code more compact and often easier to read. Let's filter for numbers greater than 5.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Use a lambda function directly inside filter()
greater_than_five = filter(lambda x: x > 5, numbers)
print(list(greater_than_five))
[6, 7, 8, 9, 10]
The lambda lambda x: x > 5 is the test function. It returns True for numbers greater than 5. This is a very common and powerful pattern.
Filtering with None as the Function
You can pass None as the first argument to filter(). This has a special meaning.
When the function is None, filter() removes all items that evaluate to False. These are known as "falsy" values.
Falsy values include False, 0, None, empty strings "", and empty containers like [].
mixed_data = [0, 1, False, True, "hello", "", [], [1,2], None]
# Filter out all falsy values
truthy_values = filter(None, mixed_data)
print(list(truthy_values))
[1, True, 'hello', [1, 2]]
This is a quick way to clean a list. It removes all empty or false items in one line.
Practical Examples of the Filter Function
Let's look at more realistic examples. This will show you how filter() solves common problems.
Example 1: Filtering a List of Dictionaries
Imagine you have a list of user dictionaries. You want to find all active users.
users = [
{"name": "Alice", "active": True},
{"name": "Bob", "active": False},
{"name": "Charlie", "active": True},
{"name": "Diana", "active": False}
]
# Lambda checks the 'active' key in each dictionary
active_users = filter(lambda user: user["active"] == True, users)
print(list(active_users))
[{'name': 'Alice', 'active': True}, {'name': 'Charlie', 'active': True}]
Example 2: Filtering Strings
You can filter strings based on their content. Let's find words that start with a specific letter.
words = ["apple", "banana", "apricot", "blueberry", "avocado", "cherry"]
# Find words starting with 'a'
a_words = filter(lambda word: word.startswith('a'), words)
print(list(a_words))
['apple', 'apricot', 'avocado']
Filter vs. List Comprehension
Python offers another way to filter: list comprehensions. They are often more popular.
Let's compare both methods for the same task. We'll filter for even numbers again.
numbers = [1, 2, 3, 4, 5, 6]
# Using filter() with lambda
result_filter = list(filter(lambda x: x % 2 == 0, numbers))
# Using a list comprehension
result_comp = [x for x in numbers if x % 2 == 0]
print("Filter result:", result_filter)
print("Comprehension result:", result_comp)