Python map() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in type

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.

python
results = map(function, iterable)
results = map(function, iterable1, iterable2)
CallWhat 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
python
map(function, iterable, /, *iterables)
# class map: returns an iterator

Parameters

ParameterDescription
functionRequired. Called with one item from each iterable.
iterableRequired. The items to transform.
*iterablesOptional. More iterables; the function then takes that many arguments.

Return Value

Return ValueDescription
mapA lazy iterator of results.

Python Examples

Example 1 — Apply Built-in Functions to Every Item

Map len(), str.upper, and int() over lists of strings.

python
words = ["apple", "kiwi", "banana"]

lengths = map(len, words)
print(list(lengths))
print(list(map(str.upper, words)))
print(list(map(int, ["7", "42", "-3"])))

How It Works

  1. Pass the function, do not call it. map(len, words) hands over len itself, without parentheses.
  2. Methods work too. str.upper is called with each word as its argument.
  3. Type conversion. Mapping int over strings is a common way to parse numbers.
  4. 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.

python
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

  1. Two arguments. With two iterables, the lambda receives a price and a quantity together.
  2. Shortest wins. The second list has two items, so pow() runs only twice: 2 ** 3 and 3 ** 2.
  3. No intermediate list. sum() consumes the map directly: 30 + 20 + 60.
  4. 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.

python
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"]])

How It Works

  1. Nothing runs at first. Creating the map prints nothing from shout().
  2. On demand. next() processes exactly one item.
  3. Exhausted. list() takes the remaining item; the next list() finds nothing.
  4. Comprehension equivalent. The last line builds the same results eagerly in one readable expression.

Frequently Asked Questions

A lazy map iterator. Use list(map(...)) to get a list.
It prints the map object's type, not its contents. Wrap it in list() first.
Both are fine. map(func, xs) is neat with an existing function; comprehensions are clearer with lambdas or conditions.
Yes. map(f, a, b) calls f(a[i], b[i]) and stops at the shortest list.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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