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
| Parameter | Description |
|---|---|
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
print(list(map(str.upper, ["a", "b", "c"])))
print(list(map(int, ["1", "2", "3"])))
Output
['A', 'B', 'C'] [1, 2, 3]
prices = [10, 20, 30]
quantities = [2, 0, 5]
print(list(map(lambda p, q: p * q, prices, quantities)))
Output
[20, 0, 150]
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()andzip(),map()is lazy: wrap it inlist()to see the results. - With several iterables it stops at the shortest one, unless
strict=True(Python 3.14+).
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
| Python 3.14 | Added the strict parameter. |
Related
filter()· Keep only the items of an iterable for which a function returns True. Returns a lazy iterator, not a list.zip()· Iterate over several iterables in parallel, producing tuples of their items. Stops at the shortest unless strict=True.list()· Create a list: an ordered, changeable sequence. Called with an iterable, it copies its items into a new list.sum()· Add up the numbers in an iterable, plus an optional start value. Floats are summed with extra precision since Python 3.12.- Python Lambda · tutorial
- Python List Methods and Comprehensions · tutorial