On this page
  1. filter
  2. Import & Availability
  3. Overview & Signature
  4. Usage
  5. Gotchas
  6. REPL Inspect

filter

Prev: exit | Up: __builtins__ | Next: float
Rel: all, any, enumerate, iter, aiter
Ref: docs.python.org — filter
Tags: #public #python3 #builtins


Import & Availability

# Built-in (available globally without import)
from builtins import filter

Overview & Signature

filter(function, iterable, /)

Return an iterator yielding those items of iterable for which function(item) is true. If function is None, return the items that are true.

Usage

evens = list(filter(lambda x: x % 2 == 0, range(6)))
assert evens == [0, 2, 4]
# predicate=None filters out falsy values
assert list(filter(None, [0, "", "ok", None, 5])) == ["ok", 5]

Gotchas

REPL Inspect

help(filter)

Linked from

Related notes