How map works
Control when it runs and what it returns.
A map waits until you ask.
map() creates an iterator: it gives you results one at a time and remembers its place. Creating it does not run your function. This is called lazy evaluation.
def double(x):
return x * 2
m = map(double, [1, 2, 3])m is a map iterator1Next2Waiting3Waiting
None yetRecent requests
No calls to double yet. Ask for one result to begin.
Try next(m), then list(m), then list(m) again. Each request continues from the current position.
One map, one pass.
After the last item, the map is exhausted. list(m) gives []; next(m) raises StopIteration, meaning there is no next value. Make a fresh map to start again.
How do I keep the results?
Save the list. That list can be read again, even after the map is exhausted.
values = list(map(double, [1, 2, 3]))
print(values) # [2, 4, 6]
print(values) # [2, 4, 6]print(m) shows a map object, not its results. You also cannot use m[0] or len(m). Collect a list first if you need indexing or a length.
Do I always need list()?
No. A loop, sum(), or tuple() can also request results. For example, this adds the doubled values without building an output list:
sum(map(double, [1, 2, 3])) # 12One item from each input.
With two inputs, map() takes a pair at each position. Your function must accept two arguments.
def add(x, y):
return x + y
| Position | 1 | 2 | 3 |
|---|---|---|---|
a | 1 | 2 | 3 |
b | 10 | 20 | 30 |
add | 1 + 10 | 2 + 20 | 3 + 30 |
| Result | 11 | 22 | 33 |
a = [1, 2, 3]
b = [10, 20, 30]
list(map(add, a, b))
[11, 22, 33]Three pairs, three calls to add. Each pair produces one result.
By default, map() stops when the shortest input ends. An item without a partner produces no result.
Want unequal lengths to raise an error?
In Python 3.14 and later, add strict=True. Unequal lengths raise ValueError when the mismatch is reached while requesting results.
list(map(add, a, b, strict=True))Try it with the shortened input above. Earlier pairs are processed, but list() cannot finish and returns no list.
Different functions. Same pattern.
Use a function you define, a built-in function, or a small lambda. Each receives one item from a single input.
"10""20""30"inton each item102030list(map(int, ["10", "20", "30"]))
[10, 20, 30]Quotes mark strings. The built-in int function converts each numeric string to an integer.
Can the input be something other than a list?
Yes. An iterable is a source you can take items from one at a time. Tuples, strings, and range() work too. A string supplies one character at a time:
list(map(str.upper, "hi"))
# ['H', 'I']Compare that with ["hi"]: a list containing one word gives one result, ['HI'].
Every return value is a result.
A function can return True or False. map() keeps those results too.
def is_even(x):
return x % 2 == 0
x % 2 == 0 checks whether a number is even.
1False2True3False4Truelist(map(is_even, [1, 2, 3, 4]))
[False, True, False, True]map returns the answers to the test: one Boolean for every input.
filter() uses the same test to keep matching original items: here, 2 and 4. map() returns what the function returns, including False or None.
Keep this mental model
Ask for a result → take the next input → call the function.
The map remembers where it stopped.
What comes out?
1 of 5You know what happens next.
Results are produced on request.
Each map remembers its position.
Multiple inputs are paired in order.
Review the lesson