Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Function

filter

filter() constructs an iterator containing elements from an iterable that pass a function's truth test. With None as the function, it removes false-valued elements.

How it works

filter() lazily iterates over an iterable and yields each element whose predicate produces a truthy result. Passing None uses each element's own truth value, so false elements are excluded; the result can be consumed like any other iterator.

Syntax

filter(function, iterable, /)

Parameters

NameTypeDescription
functioncallable or NoneA callable tested against each element; use None to test elements by their own truth value.
iterableiterableA sequence, iterable container, or iterator whose elements are examined.

Returns

An iterator over elements of iterable for which function is true.

Key points

  • filter() produces an iterator containing only elements accepted by the supplied function.
  • The second argument may be a sequence, an iterable container, or an existing iterator.
  • When the function is None, false-valued elements are omitted.
  • A non-None function is called for each element to decide whether that element is retained.
  • The None case tests each element directly instead of invoking a callable.
  • Use itertools.filterfalse() when the desired output is the elements rejected by a predicate.

Common mistakes

  • With None, filtering uses each element itself as the truth test rather than calling a predicate.
  • The operation is equivalent to a generator expression, so it does not create a list automatically.

Example: Keep matching strings

active_names = list(filter(lambda name: name.startswith("A"), ["Ada", "Grace", "Alan", "Linus"]))
print(active_names)

Converts the lazy filter iterator to a list containing names that start with A.

When to use it

Predicate-based selection

Keep records satisfying a validation or eligibility predicate before processing them.

Remove false values

Remove empty strings, zeroes, None values, or other false-valued items from an iterable.

Lazy pipeline

Process large or streaming inputs incrementally without immediately materializing a list.

Interview questions

QWhat does filter() do, and what kinds of iterables can it accept?

filter() takes a function and an iterable, then constructs an iterator containing elements for which the function is true. The iterable can be a sequence, container, or iterator.

Follow-up: What happens when the function is None? Each element is tested directly, and false elements are removed.

QDoes filter() return a list?

The result is an iterator, so it is evaluated as it is consumed. Convert it with list() or another consumer when a materialized collection is needed.

Follow-up: How could the same behavior be expressed without filter()? Use a generator expression with the corresponding condition.

QWhat is the effect of passing None as the function?

With function set to None, filter() applies the identity behavior: elements whose truth value is false are omitted. This is equivalent to iterating with an if item condition.

Follow-up: What is the complementary standard-library operation? itertools.filterfalse() selects elements for which the predicate is false.

  • itertools.filterfalse()
  • map()
  • generator expressions
  • list comprehensions

Source: Built-in Functions. Checked 2026-10-15.