Home / Python / Reference / Built-in functions

Python map() function

Apply a function to every item.

Syntax

map(function, iterable, ...)

Returns an iterator: wrap it in list() to see the results. A list comprehension is often clearer: [f(x) for x in items].

Parameters

functionapplied to each item
iterablethe items

Return value

a map iterator

Example

Code

print(list(map(str.upper, ["a", "b"])))
print(list(map(int, "1 2 3".split())))
print([int(x) for x in "1 2 3".split()])   # the same with a comprehension

Output

['A', 'B']
[1, 2, 3]
[1, 2, 3]

Related

Learn it step by step in the free lesson Pythonic Python, with exercises that check your answers.