Built-in Functions and Types

filter()

Select input items lazily. For each item, test the result of the predicate and yield the original item if that result is truthy. Passing None as the predicate performs ordinary truth testing on the items. A filter does not transform accepted elements and is exhausted after one pass. Predicate exceptions propagate when the iterator advances.

Formal Syntax & Structure

python syntaxpython
filter(function, iterable, /)

Parameters & Attributes

NameTypeDescription
functioncallable or NoneOne-argument predicate, or None to test the input itself.
iterableiterableItems to inspect.

Return Value

filter iterator

Lazy single-pass stream of accepted original items.

Working Example

Example Snippetpython
measurements = [0, -3, 4, 8, -1]print(list(filter(lambda n: n > 0, measurements)))print(list(filter(None, measurements)))
Python Reference