Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

itertools.filterfalse

Yields items for which the predicate returns a falsy result.

Version: Python 3.14; repository image, production runtime not attested

Syntax

itertools.filterfalse(predicate, iterable, /)

Yields items for which the predicate returns a falsy result.

Passing None uses each item’s own truth value and retains only falsy items.

The iterator selects original items whose predicate result tests false. An even number gives remainder zero, so 0, 2 and 4 pass. A predicate of None uses the truth value of each item itself, keeping numeric zero and the empty string while skipping 1 and a.

import itertools as I
assert list(I.filterfalse(lambda n: n % 2, range(5))) == [0, 2, 4]
assert list(I.filterfalse(None, [0, 1, "", "a"])) == [0, ""]

Examples

import itertools as I
print(list(I.filterfalse(lambda n: n % 2, range(5))))
print(list(I.filterfalse(None, [0, 1, "", "a"])))

Result

[0, 2, 4]
[0, '']

The iterator selects original items whose predicate result tests false. An even number gives remainder zero, so 0, 2 and 4 pass. A predicate of None uses the truth value of each item itself, keeping numeric zero and the empty string while skipping 1 and a.

Sources