Course
Filtering is one of the most common tasks in Python, which is why Python makes it easy. One of the simplest ways to filter data is with the built-in filter()
function. As its name makes it clear, filter()
lets you pull out only the items that meet a certain condition (or conditions).
This guide walks you through how it works, when to use it, and how it compares to other filtering tools. Have a read!
What Python filter() Does
filter()
lets you pull out elements from a list, tuple, or any iterable, but only if they pass your test. You provide a function (sometimes I've seen this called a predicate) that takes each item and returns True
or False
. If the test passes (read: if your function returns True
) filter()
keeps it. Otherwise, it skips the item. This is pretty self-explanatory, so let's continue.
Python filter() Syntax
Here's the format:
filter(function, iterable)
-
function
: Specifies the test you want to apply to each item. It should take one argument and returnTrue
orFalse
. -
iterable
: Provides a list, tuple, string, or any object you can loop over.
When you call filter()
, it returns a special filter object, not a list or tuple directly. To actually work with the filtered results, you'll usually wrap the output in a list or tuple. This design choice keeps filter()
efficient, but it's something to keep in mind as you write your code.
Python filter() Examples
Let’s practice a bit.
Python filter() with a custom function
Let's use filter()
with a function we write for ourselves.
Suppose you want to keep only the even numbers from a list:
def is_even(num):
return num % 2 == 0
numbers = [1, 2, 3, 4, 5, 6]
result = filter(is_even, numbers)
print(list(result))
This will print:
[2, 4, 6]
Notice how is_even
checks if a number is divisible by 2. filter()
uses our custom function to test each item and keep the ones that pass our test.
Python filter() with lambda functions
Of course, not every filter needs a full function definition. Sometimes, your filtering rule is so simple or so specific to one spot that defining a named function is too much work.
Here’s the same even-number example, but using a lambda for a concise, in-place test:
numbers = [1, 2, 3, 4, 5, 6]
result = filter(lambda x: x % 2 == 0, numbers)
print(list(result))
You'll get the same output as before.
Using lambda with filter()
is especially common when you want to keep your code short and focused. It's a great fit for one-off filtering tasks, letting you express your rule right where you need it.
Python filter() with None as the function
Sometimes, you want to filter()
out empty or false values. Things like 0
, empty strings, None
, or False
, and we want to do this without writing a custom function.
Python has a trick for this: If you set the function argument in filter()
to None
, filter()
will automatically remove all items that Python considers False
.
Here's an example:
items = [0, 1, '', 'hello', [], [2, 3], None, False, True]
result = filter(None, items)
print(list(result))
This will print:
[1, 'hello', [2, 3], True]
Filtering text data
Now let’s apply filter()
to a practical, everyday problem. Suppose you have a list of words and only want those that start with 'a'. This kind of targeted filtering comes up all the time when processing text.
words = ['apple', 'banana', 'apricot', 'cherry', 'avocado']
result = filter(lambda word: word.startswith('a'), words)
print(list(result))
You'll get:
['apple', 'apricot', 'avocado']
Filtering with multiple conditions
Sometimes, your filtering criteria get more complex. What if you need items that meet several conditions? Here’s how you can do it using a lambda. In this example, we are finding numbers that are both even and greater than 10.
numbers = [4, 10, 12, 15, 20, 23, 28]
result = filter(lambda x: x % 2 == 0 and x > 10, numbers)
print(list(result))
This prints:
[12, 20, 28]
(If I were to have even more conditions, I would consider switching from a lambda to a named function for clarity.)
filter() vs. List Comprehension
List comprehensions are another popular way to select items. Both approaches let you filter data, but each has its own style.
Here’s the even-number example again, this time using a list comprehension:
numbers = [1, 2, 3, 4, 5, 6]
result = [x for x in numbers if x % 2 == 0]
print(result)
[2, 4, 6]
Both filter()
and list comprehensions will get the job done. List comprehensions are often easier to read for straightforward filters, especially when the rule is short and simple.
On the other hand, filter()
works well when you’re already working with functions, or when you want to chain together multiple processing steps.
More Advanced Uses
Let's try to find some more interesting cases:
filter() with other iterables
Just like we did with lists, filter()
also works with any iterable, like tuples, sets - even generator expressions. This is good to know because your data won't always be in list form.
Here's how you can filter a tuple:
numbers = (10, 15, 20, 25, 30)
result = filter(lambda x: x > 20, numbers)
print(tuple(result))
This will print:
(25, 30)
Just remember to convert the filter object to the type you want (list, tuple, etc.) when you need to use the results directly.
Now, I also mentioned generators so, to be thorough, I'll show an example of filtering with generators, also:
numbers = (x for x in range(10))
result = filter(lambda x: x % 2 == 0, numbers)
print(list(result))
This is a generator because this line: numbers = (x for x in range(10))
creates a generator object. (It's lazy and doesn’t generate all the values at once.)
The final line: print(list(result))
forces evaluation by converting the lazy filter object into a list.
Combining filter() with map()
What if you want to both filter and transform your data, such as, for our next example, squaring only the even numbers in a list?
Python makes this easy by letting you combine filter()
and map()
. First, filter()
picks out the items you want, and then map()
applies another function to each one.
Here's that in practice:
numbers = [1, 2, 3, 4, 5, 6]
evens = filter(lambda x: x % 2 == 0, numbers)
squared = map(lambda x: x ** 2, evens)
print(list(squared))
This prints:
[4, 16, 36]
By chaining filter()
and map()
, you can build clear, readable data pipelines, by first selecting the items you care about, then transforming them as needed. There will be opportunities to use this pattern in your own work.
Things to Watch Out For
As handy as filter()
is, there are a few mistakes people make:
-
filter()
returns a filter object, not a list. This can trip you up if you expect to see results right away. Always wrap it in a list (or tuple) if you need to see or reuse the results. -
filter()
only works with one iterable at a time. If you want to filter based on multiple lists, you'll need to zip them together or use another approach. -
filter()
is lazy. It doesn't actually process the data until you convert it to a list, tuple, or loop over it. Keep this behavior in mind when debugging.
Conclusion
As you've seen, Python's filter()
function gives you a quick way to clean up data by pulling out only what matters. filter()
helps if you're writing a one-off lambda or plugging in a named function.
If you’re unclear on how to use filter()
, or if you feel like that was simple enough and want to keep learning, we have great resources to help. I recommend our Python Programming Fundamentals skill track.

I'm a data science writer and editor with contributions to research articles in scientific journals. I'm especially interested in linear algebra, statistics, R, and the like. I also play a fair amount of chess!
Python Filter FAQs
What does the Python filter() function do?
The filter()
function takes a function and an iterable and returns a new iterable with only the items that pass a test. The test function should return True
for items you want to keep and False
for those you want to skip.
Why do I need to wrap the result of filter() in list() or tuple()?
filter()
returns a filter object, which is an iterator. To view or reuse the filtered results, you’ll typically convert it into a list or tuple using list()
or tuple()
. This step is essential for printing, debugging, or passing the result to other functions.
Can I use filter() without writing a custom function?
Yes. You can use a lambda function for short, one-off tests, or pass None
as the function argument to automatically remove all "falsey" values like 0
, ''
, None
, and False
.
How does filter() compare to list comprehensions?
Both filter()
and list comprehensions allow you to filter data, but list comprehensions are often more readable for simple conditions. filter()
is useful when reusing logic via functions or chaining with tools like map()
.
Can I apply multiple conditions with filter()?
Absolutely. You can combine multiple conditions using logical operators inside your test function or lambda. For complex logic, it’s often better to use a named function to keep the code readable.