Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Filtering and transformations

Filtering keeps original values whose predicate is true, while mapping emits the transformed value for every input it receives.

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

Syntax

map(transform, filter(keep, values))

Filtering keeps original values whose predicate is true, while mapping emits the transformed value for every input it receives.

Both built-ins return lazy iterators and their order can change the result. A filter with None uses truth testing and therefore drops numeric zero along with None.

The example preserves zero with an explicit missing-value predicate.

Examples

source = [None, 0, -2, 3]
values = filter(lambda x: x is not None, source)
print(list(map(abs, values)), list(values))
print(list(filter(None, source)))
print(list(filter(lambda x: x > 0, map(abs, [-2, 3]))))

Result

[0, 2, 3] []
[-2, 3]
[2, 3]

The example preserves zero with an explicit missing-value predicate.

Sources