filter()
filter(function, iterable) filter object (iterator) · Updated March 13, 2026 · Built-in Functions The built-in filter() function constructs an iterator from elements of an iterable for which a given function returns True. It is one of Python’s functional programming primitives and provides a declarative way to select items from a sequence without writing an explicit loop.
Syntax
filter(function, iterable)
When function is not None, it is called for each element in iterable. Only elements for which the function returns a truthy value are included in the result. When function is None, all falsy elements are removed.
Parameters
| Parameter | Type | Description |
|---|---|---|
function | callable or None | A function that accepts one argument and returns a truthy or falsy value. If None, identity is assumed, filtering out falsy values. |
iterable | iterable | Any Python iterable (list, tuple, generator, etc.) whose elements will be tested by function. |
Returns: A filter object, which is a lazy iterator. Wrap it in list(), tuple(), or iterate over it directly to consume the results.
Examples
Basic usage
Filter a list of integers to keep only even numbers:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens)
# [2, 4, 6, 8, 10]
Filter strings by length:
words = ["hi", "hello", "hey", "greetings", "yo"]
long_words = list(filter(lambda w: len(w) > 3, words))
print(long_words)
# ['hello', 'greetings']
With None
When None is passed as the function, filter() removes all falsy values (0, "", None, False, empty collections):
data = [0, 1, "", "hello", None, True, False, [], [1, 2]]
truthy = list(filter(None, data))
print(truthy)
# [1, 'hello', True, [1, 2]]
This is a concise way to strip empty strings and None values from a list:
raw_input = ["alice", "", "bob", None, "charlie", ""]
cleaned = list(filter(None, raw_input))
print(cleaned)
# ['alice', 'bob', 'charlie']
Common pattern
Using filter() with a named function for readability:
def is_positive(n):
return n > 0
readings = [-3, 0, 5, -1, 8, 2, -7]
positive_readings = list(filter(is_positive, readings))
print(positive_readings)
# [5, 8, 2]
Filtering dictionaries from a list based on a key:
users = [
{"name": "Alice", "active": True},
{"name": "Bob", "active": False},
{"name": "Charlie", "active": True},
]
active_users = list(filter(lambda u: u["active"], users))
print([u["name"] for u in active_users])
# ['Alice', 'Charlie']
Common Patterns
filter vs. list comprehension: A list comprehension is often preferred for readability, but filter() can be more efficient when using a built-in or C-implemented function because it avoids the overhead of a Python-level loop.
# Equivalent approaches
evens_filter = list(filter(lambda x: x % 2 == 0, range(100)))
evens_comp = [x for x in range(100) if x % 2 == 0]
Chaining with map: filter() and map() compose naturally for pipeline-style data processing:
raw = [" alice ", "BOB", " Charlie", ""]
result = list(map(str.strip, filter(None, raw)))
print(result)
# ['alice', 'BOB', 'Charlie']
Lazy evaluation: Because filter() returns an iterator, it works well with large or infinite sequences without consuming memory for all results at once:
import itertools
def is_prime(n):
if n < 2:
return False
return all(n % i != 0 for i in range(2, int(n**0.5) + 1))
first_10_primes = list(itertools.islice(filter(is_prime, itertools.count(2)), 10))
print(first_10_primes)
# [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]