Python map() Function

Short answer
map(function, iterable, ...) calls function on every item of the iterable and returns an iterator of the results.

map() is a quick way to transform a whole collection: convert strings to numbers, clean up text, or apply the same calculation to every value. You give it a function and some data, and it gives back the transformed values one at a time.

The result is lazy, so nothing is calculated until you loop over it or call list(). For a longer tour that also covers filter() and reduce(), see map, filter and reduce.

Quick facts
Syntaxmap(function, iterable, *iterables)
Parametersfunction: a callable taking as many arguments as there are iterables; iterable: the data to process
ReturnsA map iterator
Stops whenThe shortest iterable is exhausted
Time complexityO(n), evaluated lazily

How map() applies a function

map(f, items) produces f(item) for each item in order. Pass the function itself without parentheses: map(int, values), not map(int(), values). Any callable works: a built-in like str.upper, your own function or a lambda.

Python
words = ["cart", "pen", "book"]
print(list(map(len, words)))
print(list(map(str.upper, words)))

Using map() with more than one iterable

If you give several iterables, the function receives one item from each per call, like zip(). The iteration stops when the shortest iterable ends.

Python
prices = [10, 20, 30]
qty = [1, 2, 3]
print(list(map(lambda p, q: p * q, prices, qty)))

map() vs list comprehension

Both transform every item. A list comprehension is usually more readable and can include a condition. map() shines when you already have a named function to apply, such as int or str.strip, and when you want lazy evaluation for large data.

Aspect`map()`List comprehension
ResultLazy iteratorA real list
Readability with a lambdaHarder to readUsually clearer
Built-in functionmap(int, items) is very short[int(x) for x in items]
Add a filter conditionNeeds filter() tooAdd if ... directly
Memory useOne item at a timeWhole list at once

Examples you can run

Press Run, then edit the code and run it again.

Convert strings to integers

A very common use: input values arrive as text.

Convert strings to integers
raw = ["4", "8", "15"]
nums = list(map(int, raw))
print(nums)
print(sum(nums))
Expected output[4, 8, 15]
27

Clean text with a method

str.strip is passed without parentheses and called on each item.

Clean text with a method
names = ["  asha ", "ravi  ", " meera"]
print(list(map(str.strip, names)))
Expected output['asha', 'ravi', 'meera']

Apply a lambda

A lambda is handy for a one-off calculation such as adding tax.

Apply a lambda
prices = [100, 250, 80]
with_tax = map(lambda p: round(p * 1.18, 2), prices)
print(list(with_tax))
Expected output[118.0, 295.0, 94.4]

Map is lazy

Nothing runs until you consume the iterator. Here the function prints as it works.

Map is lazy
def double(n):
    print("doubling", n)
    return n * 2

m = map(double, [1, 2])
print("created")
print(list(m))
Expected outputcreated
doubling 1
doubling 2
[2, 4]

Common mistakes

Printing the map object

✗ Wrong

print(map(int, ["1", "2"]))
<map object at 0x...>

✓ Right

print(list(map(int, ["1", "2"])))

A map is an iterator. Use list(...) to see the values.

Calling the function inside map

✗ Wrong

list(map(int(), ["1", "2"]))
TypeError: 'int' object is not callable

✓ Right

print(list(map(int, ["1", "2"])))

Pass the function without parentheses. int() already returns the number 0.

Expecting side effects without consuming

✗ Wrong

m = map(print, ["a", "b"])
Nothing is printed, because `map()` is lazy.

✓ Right

for x in ["a", "b"]:
    print(x)

Use a plain for loop for side effects such as printing.

Calling len() on a map

✗ Wrong

m = map(int, ["1", "2"])
print(len(m))
TypeError: object of type 'map' has no len()

✓ Right

m = list(map(int, ["1", "2"]))
print(len(m))

Convert to a list first.

Python map() Function: questions people ask

What does map() do in Python?

It applies a function to every item of an iterable and returns an iterator of the results. For example, map(int, ["1", "2"]) yields 1 then 2.

Does map() return a list in Python 3?

No. In Python 3 it returns a lazy map object. Wrap it in list() to get a list.

Should I use map() or a list comprehension?

Use a list comprehension when you need a condition or the logic is more than a simple function call. Use map() with an existing function such as int or str.strip.

Keep learning

Step-by-step lessons:
Python map, filter and reducePython Lambda FunctionsPython List Comprehensions

Related:
Python filter() FunctionPython zip() FunctionPython sum() FunctionPython Lambda Functions

More in built-in functions:
Python print() FunctionPython len() FunctionPython range() FunctionPython type() FunctionPython int() FunctionPython float() FunctionPython str() FunctionPython input() Function See all →