Learn by reading through in order

map() — Apply a Function to Every Element of a List at Once

Apply a function to every element of a list with Python's map(), and decide when to reach for it vs a comprehension — all in your browser.

In the previous article on decorators, you saw how to wrap extra behavior around a function. This time, you'll shift gears and look at the built-in map() function — the tool you reach for when you want to apply the same function to every element of a list at once.

map() pairs naturally with lambdas, and the underlying mechanism is similar to a list comprehension. These days, comprehensions are usually the recommended choice over map().

What map() Is — A Higher-Order Function That Hits Every Element

map(function, iterable) applies the first argument's function to every element of the second argument (a list, tuple, etc.) and returns a map object that lets you pull out results one at a time. You can either iterate it with for, or wrap it in list() to materialize the whole thing as a list.

Because its first argument is a function, map() itself counts as a higher-order function.

How map() works
[1, 2, 3, 4, 5]lambda x: x ** 2<map object>(iterator)[1, 4, 9, 16, 25]feed each elementmap()pull out with list()
Each element of the second argument is fed through the first argument's function, and the results come back as a map object. Wrap it in list() to materialize the contents.
numbers = [1, 2, 3, 4, 5]

squared = map(lambda x: x ** 2, numbers)
print(type(squared))   # <class 'map'>
print(squared)         # <map object at 0x...>

# Pull values out with list()
print(list(squared))   # [1, 4, 9, 16, 25]

# Built-in functions work too (convert a list of strings to int)
str_nums = ["10", "20", "30"]
print(list(map(int, str_nums)))   # [10, 20, 30]

A map object is single-use

The return value of map() is an iterator — the same kind you get from a generator — and once you've walked through it, it's empty. Call list(squared) a second time and you'll get []. If you need to use the results more than once, do list(map(...)) upfront and work with the list.

Build a list of tax-included (10%) prices from a prices list of product prices.

① Declare prices = [100, 250, 480, 1200].

② Multiply each element by 1.1, convert to int, build the resulting list, and assign it to tax_included.

③ Print the result with print(tax_included).

(When the answer is correct, the explanation will appear.)

Python Editor

Run code to see output

Passing Multiple Iterables at Once

From the second argument onward, map() accepts multiple iterables. In that case, the first argument's function needs to accept one element from each at the matching position. Write map(function, A, B) and the function gets applied to the pair A[0] and B[0], then A[1] and B[1], and so on.

For example, to multiply two numeric lists element-wise, map(lambda a, b: a * b, A, B) gives you a list of pairwise products.

A = [1, 2, 3]
B = [10, 20, 30]

# Multiply elements at the same index
print(list(map(lambda a, b: a * b, A, B)))   # [10, 40, 90]

# Same with three iterables
def calculate(x, y, op):
    return x + y if op == "plus" else x - y

xs = [10, 20, 30]
ys = [3,  3,  3]
ops = ["plus", "minus", "plus"]

print(list(map(calculate, xs, ys, ops)))     # [13, 17, 33]
Passing two lists to map()
lambda a, b: a * bA = [1, 2, 3]B = [10, 20, 30](1, 10)(2, 20)(3, 30)104090first1*102*203*30
Elements of A and B at the same index are paired up and fed into the lambda lambda a, b: a * b.

Mismatched lengths stop at the shortest

When the iterables you pass have different lengths, map() stops at the shortest one. For instance, map(f, [1, 2, 3, 4], [10, 20]) only runs twice. If you want explicit alignment, check the lengths in advance, or pair map() with `zip()` depending on your needs.

From a list of product names and a list of counts, build a list of "name × count" strings using map().

① Declare items = ["apple", "orange", "grape"] and counts = [3, 5, 2].

② Wrap map(lambda name, n: f"{name} × {n}", items, counts) in list(...) and assign it to labels.

③ Print each one on its own line with for label in labels: print(label).

Python Editor

Run code to see output

map() vs List Comprehension — Which Should You Use?

Almost everything map() can do, a list comprehension can do too. Python's official docs and books like Fluent Python recommend preferring comprehensions in new code, and in modern Python, comprehensions are the standard form.

map() used to be the default in older code, but its role has shrunk to cases where you can hand off an existing function name without writing a lambda (think map(int, str_nums)). Let's line up the differences side by side.

GoalWith map()With comprehension
Square every elementlist(map(lambda x: x ** 2, nums))[x ** 2 for x in nums]
Convert str to intlist(map(int, str_nums))[int(s) for s in str_nums]
Two lists at oncelist(map(f, A, B))[f(a, b) for a, b in zip(A, B)]
nums = [1, 2, 3, 4, 5]

# map() version
print(list(map(lambda x: x ** 2, nums)))   # [1, 4, 9, 16, 25]

# Comprehension version (preferred)
print([x ** 2 for x in nums])              # [1, 4, 9, 16, 25]

# When you can pass a function name directly, map() looks cleaner
str_nums = ["10", "20", "30"]
print(list(map(int, str_nums)))            # [10, 20, 30]
print([int(s) for s in str_nums])          # [10, 20, 30]

# When filtering, comprehensions are far more concise
print([x for x in nums if x % 2 == 0])     # [2, 4]

When in doubt, reach for a comprehension

Whether you want to transform, filter, or do both, a comprehension lets you write it all naturally inside the same brackets. If you commit to using map() only when you're handing off an existing function name as-is, your code stays consistent. It's lighter on both the writer and the reader, so prefer comprehensions in new code.

Convert a list of input strings into a list of integers in two ways: with map() and with a comprehension.

① Declare inputs = ["10", "20", "30", "40"].

② Wrap map(int, inputs) in list(...), assign it to via_map, and print(via_map).

③ Build the same result with [int(s) for s in inputs], assign it to via_comp, and print(via_comp).

④ Finally, run print(via_map == via_comp) to confirm the two values are equal.

Python Editor

Run code to see output
QUIZ

Knowledge Check

Answer each question one by one.

Q1What does this code print?
nums = [1, 2, 3]
print(list(map(lambda x: x + 10, nums)))

Q2Which comprehension produces the same result as list(map(int, ["1", "2", "3"]))?

Q3Which statement about map()'s return value is correct?