Q1What does this code print?nums = [1, 2, 3]
print(list(map(lambda x: x + 10, nums)))
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.
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.
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]
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.
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.
| Goal | With map() | With comprehension |
|---|---|---|
| Square every element | list(map(lambda x: x ** 2, nums)) | [x ** 2 for x in nums] |
| Convert str to int | list(map(int, str_nums)) | [int(s) for s in str_nums] |
| Two lists at once | list(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.
Knowledge Check
Answer each question one by one.
Q2Which comprehension produces the same result as list(map(int, ["1", "2", "3"]))?
Q3Which statement about map()'s return value is correct?