Last modified: Nov 19, 2026
Python filter Function Explained with Examples
The filter function is a built-in tool in Python. It helps you pick items from a collection that match a rule. You give it a rule and a list. It returns only the items that pass the rule.
This guide covers the basics and the details. You will see syntax, simple examples, and real use cases. By the end, you will know when and how to use filter in your own code.
What Is the Python filter Function?
filter takes two arguments. The first is a function that returns True or False. The second is an iterable, such as a list, tuple, or set.
The function checks each item in the iterable. Items that return True are kept. Items that return False are dropped. The result is a filter object, which is a lazy iterator.
This makes filter a clean choice for data selection. It also pairs well with map when you need to transform data. If you want to see how that works, read about the Python map function explained step by step.
Basic Syntax of filter
The syntax is simple:
filter(function, iterable)
The function is often a small helper. The iterable is the data source. If the function is None, filter keeps only items that are truthy.
A Simple Example
Let's start with a plain function. It checks if a number is even.
def is_even(n):
return n % 2 == 0
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
result = filter(is_even, numbers)
print(list(result))
[2, 4, 6, 8]
Notice the use of list around the result. The filter object does not show its values until you convert it. You can also loop over it directly.
Using filter with Lambda
Writing a full function is not always needed. A lambda works well for short rules. A lambda is a small anonymous function. If you are new to them, this guide on the Python lambda function syntax and examples explains the details.
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
result = filter(lambda n: n % 2 == 0, numbers)
print(list(result))
[2, 4, 6, 8]
This version is shorter and easier to read. Use lambda when the rule is simple and used only once.
Filtering Strings
filter works with any iterable. Here is an example with words. We keep names with more than four letters.
names = ["Ann", "Robert", "Eva", "Jonathan", "Leo"]
long_names = filter(lambda name: len(name) > 4, names)
print(list(long_names))
['Robert', 'Jonathan']
This pattern is useful for cleaning lists. You can remove empty strings or short values in one line.
Filtering with None
If you pass None as the function, filter keeps truthy items. Falsy items include 0, "", None, and empty lists.
values = [0, 1, "", "hello", None, [], [1, 2]]
clean = filter(None, values)
print(list(clean))
[1, 'hello', [1, 2]]
This is a fast way to remove empty or false values. It is common in data cleaning tasks.
Combining filter with Other Functions
You can chain filter with other built-ins. For example, you may filter a list and then find its smallest or largest value. This article on how to find max and min in a list shows helpful patterns.
scores = [45, 88, 72, 91, 60, 33]
passed = filter(lambda s: s >= 60, scores)
print(max(passed))
91
Keep in mind that filter objects are single-use. Once you consume one, it is empty. Convert it to a list if you need to reuse the data.
filter vs List Comprehension
Many Python developers use list comprehensions instead. Both work, but the style differs.
numbers = [1, 2, 3, 4, 5, 6]
# Using filter
evens_filter = list(filter(lambda n: n % 2 == 0, numbers))
# Using list comprehension
evens_comp = [n for n in numbers if n % 2 == 0]
print(evens_filter)
print(evens_comp)
[2, 4, 6]
[2, 4, 6]
List comprehensions are often more readable for simple cases. filter shines when you already have a named function. Both are valid, so pick the one that fits your style.
Common Mistakes
Beginners often forget to convert the result. Printing a filter object shows something like <filter object at 0x...>. Wrap it in list() to see the values.
Another mistake is reusing a filter object. After the first loop, it is empty. Create a new one or store the results in a list.
Also, avoid complex lambdas. If the rule is long, write a named function. Clear code beats clever code.
Practical Use Cases
filter is handy in many everyday tasks. You can remove invalid entries from user input. You can select records that meet a condition. You can clean data before analysis.
It also works well with other iterables. For example, you can pair it with zip results. This guide on the Python zip function to combine iterables shows how to build paired data first, then filter it.
names = ["Ann", "Bob", "Cara"]
ages = [17, 25, 30]
pairs = list(zip(names, ages))
adults = filter(lambda pair: pair[1] >= 18, pairs)
print(list(adults))
[('Bob', 25), ('Cara', 30)]
This approach keeps your logic short and readable. It also avoids nested loops.
Performance Notes
filter is lazy. It does not process the whole iterable at once. This can save memory on large data sets. However, for small lists, the speed difference is tiny.
In most projects, readability matters more. Use filter when it makes your intent clear. Use a comprehension when that reads better.
Conclusion
The filter function is a simple and powerful tool. It selects items from an iterable based on a rule. It works with named functions, lambdas, and even None.
Remember to convert the result to a list when you need to see or reuse it. Keep your functions small and clear. With these tips, you can use filter with confidence in your Python projects.