map() (Python)

map() applies a function to every item in a sequence and returns the results. It transforms a whole iterable without writing an explicit loop.

Python

map() returns a lazy map object, so you usually wrap it in list() to see the values. A list comprehension often does the same job more readably.

Syntax

map(function, iterable)

Examples

Python

nums = [1, 2, 3]
doubled = list(map(lambda x: x * 2, nums))
print(doubled)

Output:

[2, 4, 6]

See also

Back to the Dictionary