Built-in function

map()

Calls a function on every item of a list or other iterable and gives back the results, for example to turn a list of strings into numbers.

BeginnerEvery example below runs in your browser. Change the code and press Run.

map(function, iterable, *iterables)

map(function, items) runs the function on each item and hands back the results in the same order. map(int, ["1", "2"]) turns text into the numbers 1 and 2.

It shines with functions that already exist: int(), float(), str.upper, str.strip, abs(). With a lambda (a small unnamed function) it can do any calculation, though a list comprehension such as [p * 2 for p in prices] is often easier to read.

map() is lazy: it gives back a map object and does the work only when you loop over it. Printing it shows <map object at 0x...>, so wrap it in list(). Like other iterators, it can be used only once.

Parameters

ParameterWhat it is
function
required
The function to call on each item. It must take as many arguments as you pass iterables.
iterable
required
The items to process.
*iterables
optional
More iterables. The function then gets one item from each, side by side, and map() stops at the shortest. Python 3.14 adds strict=True, which raises an error on unequal lengths, like zip().

Return value

A map object (an iterator) that produces the results one by one.

Examples

1Convert a list of strings to numbers

Pythoneditable
cells = ["12", "7", "30"]
numbers = list(map(int, cells))
print(numbers)
print(sum(numbers))
Output
[12, 7, 30]
49

2Read numbers typed on one line

Pythoneditable
nums = list(map(float, input("Prices: ").split()))
print(nums)
print(f"Total: {sum(nums):.2f}")
Output
Prices: [4.5, 12.0, 3.25]
Total: 19.75

3Use a string method or a lambda

Pythoneditable
names = [" ana ", "BEN", "chen  "]
clean = list(map(str.strip, names))
print(clean)
print(list(map(str.title, clean)))
print(list(map(lambda n: len(n), clean)))
Output
['ana', 'BEN', 'chen']
['Ana', 'Ben', 'Chen']
[3, 3, 4]

4Two lists at once

Pythoneditable
prices = [2.5, 4.0, 1.2]
qty = [4, 2, 10]
line_totals = list(map(lambda p, q: p * q, prices, qty))
print(line_totals)
Output
[10.0, 8.0, 12.0]

5map() vs a list comprehension

Pythoneditable
temps_c = [0, 21, 37]
print(list(map(lambda c: c * 9 / 5 + 32, temps_c)))
print([c * 9 / 5 + 32 for c in temps_c])
Output
[32.0, 69.8, 98.6]
[32.0, 69.8, 98.6]

Both give the same list. Many Python programmers prefer the comprehension when it needs a lambda, and map() when a ready-made function fits.

Common mistakes

Printing the map object

map() does not give a list. Wrap it in list() to see or reuse the results.

Pythoneditable
squares = map(lambda n: n * n, [1, 2, 3])
print(type(squares).__name__)
print(list(squares))
print(list(squares))
Output
map
[1, 4, 9]
[]

Calling the function instead of passing it

Pass the function's name without brackets. map(int(), cells) calls int() first, which gives 0, and 0 is not a function.

Pythoneditable
cells = ["1", "2"]
try:
    list(map(int(), cells))
except TypeError:
    print("TypeError: 0 is not a function")
print(list(map(int, cells)))
Output
TypeError: 0 is not a function
[1, 2]

Questions people ask

How do I convert a list of strings to ints in Python?

list(map(int, strings)) or [int(s) for s in strings].

Should I use map() or a list comprehension?

Either is fine. map(int, items) is short when the function exists; a comprehension reads better when you would need a lambda, and it can also filter with if.

Why does map() return an object, not a list?

Since Python 3 it is lazy, so it can handle huge or endless data without storing everything. Use list() when you want all results at once.

Spotted a mistake on this page? Tell us