Home Python Built-in Functions map() Python map() Function Definition In Python, map(function, iterable) calls function on each item and gives you the results. map(int, ["1", "2"]) produces 1 and 2. With several iterables, the function receives one item from each, and mapping stops at the shortest.
map() is lazy : it returns an iterator and does the work only when you loop over it or wrap it in list() . Like every iterator, it can be consumed once. Printing the map object itself just shows its type, not the values.
map is a built-in type (a class) that you call like a function: each call creates a new map object. Like every built-in, it lives in the builtins module and is always available without an import.
Syntax Pass a function and one or more iterables.
results = map(function, iterable)
results = map(function, iterable1, iterable2) Call What it does list(map(len, ["a", "bb"]))Returns [1, 2]. list(map(int, "123"))Returns [1, 2, 3]. map(lambda x: x * 2, xs)Doubles every item, lazily. sum(map(len, words))Total length without a list.
A list comprehension like [f(x) for x in xs] does the same thing eagerly and is often easier to read, especially with conditions.
Library declarations map(function, iterable, /, *iterables)
# class map: returns an iterator Parameters Parameter Description function Required. Called with one item from each iterable. iterable Required. The items to transform. *iterables Optional. More iterables; the function then takes that many arguments.
Return Value Return Value Description map A lazy iterator of results.
Python Examples 1. Transform items 2. Several iterables 3. Lazy evaluation Example 1 — Apply Built-in Functions to Every Item Map len() , str.upper, and int() over lists of strings.
words = ["apple", "kiwi", "banana"]
lengths = map(len, words)
print(list(lengths))
print(list(map(str.upper, words)))
print(list(map(int, ["7", "42", "-3"]))) [5, 4, 6]
['APPLE', 'KIWI', 'BANANA']
[7, 42, -3] How It Works Pass the function, do not call it. map(len, words) hands over len itself, without parentheses.Methods work too. str.upper is called with each word as its argument.Type conversion. Mapping int over strings is a common way to parse numbers.Shown with list(). Each map object is wrapped in list() to display the results.Example 2 — Combine Several Iterables with One Function Multiply prices by quantities, and see that map stops at the shortest input.
prices = [10, 20, 30]
quantities = [3, 1, 2]
totals = list(map(lambda p, q: p * q, prices, quantities))
print(totals)
print(list(map(pow, [2, 3, 4], [3, 2])))
print(sum(map(lambda p, q: p * q, prices, quantities))) How It Works Two arguments. With two iterables, the lambda receives a price and a quantity together.Shortest wins. The second list has two items, so pow() runs only twice: 2 ** 3 and 3 ** 2.No intermediate list. sum() consumes the map directly: 30 + 20 + 60.Like zip plus a call. It behaves like [f(a, b) for a, b in zip(xs, ys)].Example 3 — Watch map() Work Lazily Use a function that prints when it runs to see when map actually does the work.
def shout(word):
print("processing", word)
return word.upper() + "!"
results = map(shout, ["hi", "bye"])
print("map created, nothing processed yet")
print(next(results))
print(list(results))
print(list(results))
print([w.upper() + "!" for w in ["hi", "bye"]]) map created, nothing processed yet
processing hi
HI!
processing bye
['BYE!']
[]
['HI!', 'BYE!'] How It Works Nothing runs at first. Creating the map prints nothing from shout().On demand. next() processes exactly one item.Exhausted. list() takes the remaining item; the next list() finds nothing.Comprehension equivalent. The last line builds the same results eagerly in one readable expression.Frequently Asked Questions What does map() return in Python 3? A lazy map iterator. Use list(map(...)) to get a list.
Why does print(map(...)) not show the values? It prints the map object's type, not its contents. Wrap it in list() first.
Should I use map() or a list comprehension? Both are fine. map(func, xs) is neat with an existing function; comprehensions are clearer with lambdas or conditions.
Can map() take several lists? Yes. map(f, a, b) calls f(a[i], b[i]) and stops at the shortest list.
How do I read several numbers with map()? nums = list(map(int, input().split())) converts each space-separated piece to an int.
Next: max() Largest item of an iterable or of several arguments.
max() → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
14 people found this page helpful
Helpful Share Copy link Suggestion