Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python filter usage: Syntax, examples, and pitfalls

Learn how to use Python's filter() function correctly: syntax, lazy evaluation, common pitfalls, and when a list comprehension is a better choice.

~4 min readPython
#python#filter#functional-programming#iterators
A funnel-shaped diagram filtering a stream of data items, with some items passing through and others being rejected, representing Python's filter() function.

What filter() Does and How Its Signature Works

Python's filter() is a built-in function. It normally takes a predicate function and an iterable, and returns an iterator that yields only the items for which the predicate returns a truthy value. Practical use of filter() starts with its signature:

python
filter(function, iterable)

The first argument is usually a callable that receives each item and returns a truthy or falsy value. If you pass None as the first argument instead, filter() removes falsy items; that behavior is covered below. The second argument is any iterable: a list, tuple, set, generator, or even a custom object implementing __iter__.

A key detail is that filter() returns an iterator, not a list. This means the filtering happens lazily — items are evaluated one at a time as you consume the iterator, rather than all at once when you call filter().

python
numbers = [1, 2, 3, 4, 5, 6] even = filter(lambda n: n % 2 == 0, numbers) print(even) # <filter object at 0x...> print(list(even)) # [2, 4, 6]

If you print the filter object directly, you see its repr, not the filtered items. You need to convert it to a list or iterate over it to actually consume the results.

The None Predicate: Filtering Out Falsy Values

One of the less obvious behaviors of filter() is what happens when you pass None as the predicate. Instead of raising an error, filter(None, iterable) removes all falsy items — None, 0, 0.0, "", [], {}, False, and empty collections.

python
values = [0, 1, "", "hello", None, [], [1, 2], False, True] truthy = list(filter(None, values)) print(truthy) # [1, 'hello', [1, 2], True]

This is a concise way to strip falsy values from a sequence, but it has a subtlety: it removes 0 and False too. If your data contains legitimate zero values or boolean False that you want to keep, filter(None, ...) is the wrong tool. You need an explicit predicate that defines exactly what counts as "keep."

python
# Keep only non-None values, preserving 0 and False cleaned = [v for v in values if v is not None]

Using filter() with Lambda Functions

The most common pattern is passing a lambda as the predicate. This works well when the condition is short and self-contained:

python
scores = [58, 72, 91, 45, 88, 63] passed = list(filter(lambda s: s >= 60, scores)) print(passed) # [72, 91, 88, 63]

When the condition is more complex, a named function is usually clearer:

python
def is_available(product): return product["stock"] > 0 and not product["discontinued"] products = [ {"name": "A", "stock": 5, "discontinued": False}, {"name": "B", "stock": 0, "discontinued": False}, {"name": "C", "stock": 3, "discontinued": True}, ] available = list(filter(is_available, products))

The named function keeps the logic testable and avoids cramming a multi-condition expression into a lambda. If the predicate needs to be reused elsewhere, a named function is the right choice.

filter() vs List Comprehensions

List comprehensions can express the same filtering logic, and in many cases they are the more readable option:

python
# Equivalent results even_filter = list(filter(lambda n: n % 2 == 0, numbers)) even_comprehension = [n for n in numbers if n % 2 == 0]

The comprehension version is often preferred because it avoids the lambda and the explicit list() conversion. However, filter() has a genuine advantage when you already have a named predicate function, especially one that is reused:

python
def is_even(n): return n % 2 == 0 even_filter = list(filter(is_even, numbers)) even_comprehension = [n for n in numbers if is_even(n)]

Both are valid. The choice depends on readability and whether the predicate is a reusable named function or a one-off condition.

Lazy Evaluation and Memory Behavior

Because filter() returns an iterator, it does not build a new list in memory. This matters when you are processing large or infinite iterables.

python
def generate_ids(): n = 0 while True: yield n n += 1 # No list is materialized valid_ids = filter(lambda x: x % 100 == 0, generate_ids()) first_five = [] for _ in range(5): first_five.append(next(valid_ids)) print(first_five) # [0, 100, 200, 300, 400]

If you wrap filter() in list(), you materialize the entire result, which defeats the memory advantage. For large datasets, keep the filter lazy and consume it incrementally.

Common Mistakes and Edge Cases

Forgetting to Consume the Iterator

The most frequent mistake is treating the filter object as if it were a list. Indexing a filter object raises a TypeError, and membership tests or equality comparisons do not follow list semantics because filter objects are iterators.

python
result = filter(lambda x: x > 2, [1, 2, 3, 4]) # result[0] # TypeError: 'filter' object is not subscriptable

Convert to a list when you need indexing or repeated access.

One-Pass Iterator Behavior

A filter object is a one-shot iterator. Once you consume it, it is exhausted. Reusing the same variable after a full iteration yields nothing.

python
result = filter(lambda x: x > 2, [1, 2, 3, 4]) print(list(result)) # [3, 4] print(list(result)) # []

If you need to iterate multiple times, materialize the result into a list first.

Predicate Exceptions

If the predicate raises an exception for a particular item, the exception propagates immediately when that item is evaluated. Because evaluation is lazy, the error may not surface until you start consuming the iterator, not at the point where you call filter().

python
def safe_parse(value): return int(value) > 10 data = ["5", "12", "not-a-number", "8"] filtered = filter(safe_parse, data) # No error yet list(filtered) # ValueError: invalid literal for int() with base 10: 'not-a-number'

If you need to handle bad data gracefully, the predicate itself must catch exceptions or you should pre-validate the input.

Combining filter() with map() and Other Functional Tools

filter() is often used together with map() in a pipeline. The typical pattern is to filter first, then transform:

python
raw = ["10", "20", "abc", "30", "40"] numbers = list(map(int, filter(lambda s: s.isdigit(), raw))) print(numbers) # [10, 20, 30, 40]

This works, but for a two-stage pipeline a generator expression is often more readable:

python
numbers = [int(s) for s in raw if s.isdigit()]

For longer pipelines, itertools functions like itertools.filterfalse() provide the inverse filtering behavior — keeping items where the predicate returns False:

python
from itertools import filterfalse nums = [1, 2, 3, 4, 5, 6] odd = list(filterfalse(lambda n: n % 2 == 0, nums)) print(odd) # [1, 3, 5]

When filter() Is the Right Choice

Use filter() when:

  • You already have a named predicate function that expresses the condition clearly.
  • You want the filter(None, ...) idiom to strip falsy values without writing a custom predicate.
  • You want lazy evaluation to avoid materializing a large filtered list.
  • You are building a functional pipeline where the predicate is a reusable unit.

Use a list comprehension when:

  • The condition is a simple expression that fits naturally in the comprehension.
  • You need the result as a list immediately.
  • You want to combine filtering with transformation in one readable step.

For typical in-memory data, readability and whether lazy evaluation matters are usually stronger considerations than micro-performance differences.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.