Home Python Built-in Functions filter() Python filter() Function Definition In Python, filter(function, iterable) yields only the items for which function(item) is truthy. filter(lambda n: n > 0, nums) keeps the positive numbers and skips the rest.
If the function is None, filter keeps the items that are truthy themselves, which removes 0, "", None, and empty containers. Like map() , it is lazy and returns a one-pass iterator.
filter is a built-in type (a class) that you call like a function: each call creates a new filter object. Like every built-in, it lives in the builtins module and is always available without an import.
Syntax Pass a test function (or None) and an iterable.
kept = filter(function, iterable)
truthy = filter(None, iterable) Call What it does list(filter(str.isdigit, ["1", "a"]))Returns ['1']. list(filter(None, [0, 1, ""]))Returns [1]. filter(lambda x: x % 2, xs)Odd numbers, lazily. filterfalse(f, xs)The opposite, from itertools.
The test function should return a true or false value; filter never changes the items themselves.
Library declarations filter(function, iterable, /)
# class filter: returns an iterator Parameters Parameter Description function Required. A one-argument test, or None to test the items' own truthiness. iterable Required. The items to check.
Return Value Return Value Description filter A lazy iterator over the items that passed.
Important
The result is an iterator: print list(...) of it, and remember that it is empty after one pass. filter(None, values) also drops 0 and False, which may not be what you want for numeric data.
Python Examples 1. Keep matches 2. Drop falsy values 3. Lazy and filterfalse Example 1 — Keep Numbers and Digit Strings Filter with a lambda and with a string method.
numbers = [5, 12, 7, 20, 3]
big = filter(lambda n: n > 6, numbers)
print(list(big))
print(list(filter(str.isdigit, ["12", "abc", "7", "4.5"]))) How It Works Test per item. The lambda returns True for 12, 7, and 20.Order kept. Passing items come out in their original order.Method as test. str.isdigit rejects "abc" and "4.5" (the dot is not a digit).Items unchanged. filter only chooses items; "12" stays a string.Example 2 — filter(None, ...) Removes Falsy Values Clean a mixed list and a list of names that may be blank.
values = [0, 1, "", "text", None, [], [0], False, 3.5]
print(list(filter(None, values)))
names = [" ", "ana", "", "ben "]
cleaned = list(filter(None, (n.strip() for n in names)))
print(cleaned) [1, 'text', [0], 3.5]
['ana', 'ben'] How It Works Truthiness test. With None as the function, each item is checked with bool() .Falsy removed. 0, "", None, [], and False disappear.Non-empty is truthy. [0] is kept because the list is not empty, even though it holds a zero.Strip, then filter. Whitespace-only names become "" after strip() and are dropped.Example 3 — Lazy Filtering and Its Opposite Take a few items from a filter object, then use itertools.filterfalse for the rejected ones.
from itertools import filterfalse
def is_even(n):
return n % 2 == 0
nums = range(1, 11)
evens = filter(is_even, nums)
print(next(evens), next(evens))
print(list(evens))
print(list(filterfalse(is_even, nums)))
print([n for n in nums if is_even(n)] == list(filter(is_even, nums))) 2 4
[6, 8, 10]
[1, 3, 5, 7, 9]
True How It Works Step by step. next() pulls the first two even numbers.Remaining items. list(evens) continues from 6.The rejects. filterfalse keeps the items where the test is false: the odd numbers.Same as a comprehension. A comprehension with if produces exactly the same list.Frequently Asked Questions What does filter() return in Python 3? A lazy filter iterator. Wrap it in list() to get a list.
What does filter(None, items) do? It keeps only truthy items, removing 0, "", None, False, and empty containers.
Should I use filter() or a list comprehension? [x for x in xs if test(x)] is often clearer. filter() is concise when you already have a named test function.
How do I get the items that do not match? Use itertools.filterfalse(test, items), or a comprehension with if not test(x).
Does filter() modify the original list? No. It reads the iterable and yields the passing items; the original is unchanged.
Next: float() Convert a number or string to a floating-point number.
float() → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
12 people found this page helpful
Helpful Share Copy link Suggestion