The built-in filter(function, iterable) returns an iterator over the items of iterable for which function(item) is true. If function is None, it keeps the items that are truthy themselves. The result is lazy, so wrap it in list() when you need a list.
Many Python developers prefer a list comprehension or generator expression for the same job, so it helps to know when each is the better choice.
filter worksNonefilter versus list comprehensionsfilter worksfilter calls the predicate once per element and yields the element when the result is truthy. In Python 3 it returns a filter object (an iterator), not a list as in Python 2. An iterator can be consumed only once.
A named function is often clearer than a lambda, especially when the logic is reused.
NonePassing None removes every falsy value: 0, "", None, False and empty containers.
Be careful: this also removes legitimate zeros and empty strings.
filter versus list comprehensions| Task | filter | Comprehension |
|---|---|---|
| Keep evens | list(filter(lambda n: n % 2 == 0, xs)) | [n for n in xs if n % 2 == 0] |
| Reuse existing predicate | filter(str.isdigit, words) | [w for w in words if w.isdigit()] |
| Filter and transform | needs map too | [n * n for n in xs if n > 0] |
| Lazy result | default | use a generator expression (...) |
When you already have a predicate function, filter is concise. When you need a lambda or a transformation as well, a comprehension is usually more readable, and this is the style recommended by most Python style guides.
itertools.filterfalse(pred, iterable) yields the items for which the predicate is false, the opposite of filter.functools.partial helps build predicates with extra arguments, for example partial(operator.lt, 10).itertools.batched (Python 3.12+) and itertools.takewhile solve related problems, such as stopping at the first failure.Because filter is lazy, it works on infinite or very large streams.
Nothing is computed until islice asks for values.
<filter object at ...> instead of the values. Convert it with list() or iterate over it.filter(None, ...) drops all falsy values.Because filter accepts any callable, built-in functions and methods work as predicates without a lambda.
You can chain the two lazily, with each stage processing one item at a time.
The equivalent comprehension is [p * 2 for p in prices if p > 0], which most readers find easier to follow.
Use functools.partial or a closure to build a one-argument predicate from a more general function.
filter return a list?No. In Python 3 it returns a lazy iterator. Use list(filter(...)) to get a list.
filter faster than a list comprehension?With a built-in or C-implemented predicate it can be slightly faster, but with a lambda a comprehension is usually as fast or faster. Choose by readability and measure when it matters.
Yes. Filter its items and rebuild it: dict(filter(lambda kv: kv[1] > 0, d.items())), or use a dict comprehension.
You get an empty iterator, so list(...) returns [].
filter(function, iterable) lazily keeps the elements for which the predicate is true, and filter(None, iterable) keeps the truthy ones. Use it when you already have a predicate function, and prefer a comprehension when the condition is inline or you also need to transform the values.