Python Glossary Русский

Python 3.0+itertoolsfunction

itertools.filterfalse

itertools

Yield elements for which the predicate is false (the opposite of filter()).

Syntax

itertools.filterfalse(predicate, iterable)

As with filter, passing predicate=None means "test the element's own truthiness", so filterfalse(None, it) yields exactly the falsy items — zeros, empty strings, None. Paired with filter over one source (via tee) it is the standard way to split data into two groups by a condition.

Examples

import itertools
print(list(itertools.filterfalse(lambda x: x % 2, [1, 2, 3, 4])))   # → [2, 4]
print(list(itertools.filterfalse(None, [0, 1, '', 'a', [], [2]])))  # → [0, '', []]
print(list(itertools.filterfalse(lambda w: len(w) > 3, ['кот', 'дом', 'программа', 'и'])))  # → ['кот', 'дом', 'и']
print(list(itertools.filterfalse(lambda x: True, [1, 2, 3])))  # → []
print(list(filter(lambda x: x % 2, [1, 2, 3, 4])))  # → [1, 3]

See also

Open in the full glossarydocs.python.orgReport an inaccuracy