Python 3 reference · Built-in Functions

Python map() Function

Apply a function to every item of one or more iterables and return a lazy iterator of the results.

Python 3 Since 3.0

Syntax

map(function, iterable, *iterables, strict=False)

Parameters

ParameterDescription
function Called with one item from each iterable.
iterable, *iterables One or more iterables; with several, the function gets one argument from each.
strict optional If True, raise ValueError when the iterables have different lengths. Default: False

Return value

An iterator over the results.

Examples

Transform every itemTry it yourself
print(list(map(str.upper, ["a", "b", "c"])))
print(list(map(int, ["1", "2", "3"])))

Output

['A', 'B', 'C']
[1, 2, 3]
Combine two listsTry it yourself
prices = [10, 20, 30]
quantities = [2, 0, 5]
print(list(map(lambda p, q: p * q, prices, quantities)))

Output

[20, 0, 150]
map() vs a list comprehensionTry it yourself
print([n * n for n in [1, 2, 3]])  # the comprehension equivalent
print(sum(map(len, ["ab", "cde"])))

Output

[1, 4, 9]
5

Good to know

  • Like filter() and zip(), map() is lazy: wrap it in list() to see the results.
  • With several iterables it stops at the shortest one, unless strict=True (Python 3.14+).

Version history

VersionChange
Python 3.0Added
Python 3.14Added the strict parameter.