map()

Apply a function to every item — lazily. The functional cousin of a list comprehension.

Built-in functionPython 1.0+
Common call
list(map(int, tokens))
Returns
a lazy map iterator — wrap in list() to materialize
Replaces
a for-loop that appends a transformed value to a list
Watch out
iterator is consumed on first pass; multi-iterable form stops at the SHORTEST
map(funcfuncA function that takes as many arguments as iterables were passed. Called once per set of items.type: callable · required, *iterables)
map

Parameters

NameTypeRequiredDescription
funccallableyesA function that takes as many arguments as iterables were passed. Called once per set of items.
*iterablesiterableyesOne or more iterables. With one iterable, func is called with one arg. With multiple, called in parallel (like zip).

Return value

mapA lazy iterator that yields func(item) for each item — or func(a, b, ...) when multiple iterables are given. Not a list — you must call list() or iterate to consume it.

Common patterns

Type conversion across an iterable
The idiomatic use — convert a list of strings to ints.
values = list(map(int, input_strings))
Method call on every item
Use the unbound method — same result as a comprehension.
uppers = list(map(str.upper, names))
Parallel walk of two iterables
Map with two iterables applies func(a_i, b_i) for each pair.
sums = list(map(int.__add__, xs, ys))
When a comprehension reads better
For non-trivial expressions, a comprehension is often clearer than map + lambda.
# instead of: map(lambda x: x**2 + 1, xs)
squared = [x**2 + 1 for x in xs]

Examples

1. Double each
list(map(lambda x: x*2, [1, 2, 3]))
Returns
[2, 4, 6]
2. Convert to int
list(map(int, ["1", "2", "3"]))
Returns
[1, 2, 3]
3. Uppercase strings
list(map(str.upper, ["a", "b", "c"]))
Returns
["A", "B", "C"]
4. Two iterables
list(map(lambda a,b: a+b, [1,2,3], [10,20,30]))
Returns
[11, 22, 33]
5. Empty gives empty
list(map(str.upper, []))
Returns
[]
6. Stops at shortest
list(map(min, [1, 2, 3], [4, 5]))
Returns
[1, 2] # third pair skipped

Pitfalls

1. map() returns an ITERATOR, not a list
In Python 2 it returned a list; Python 3 made it lazy. Printing a map object shows `<map object at ...>` — call list() to materialize.
Printed iterator
print(map(str.upper, ["a"]))
<map object at 0x...>
Wrap in list
print(list(map(str.upper, ["a"])))
['A']
2. Iterator is CONSUMED on first pass
Once iterated, a map iterator is exhausted. Trying to reuse it gives an empty iterator.
Empty on second pass
r = map(int, "12345")
list(r)   # [1,2,3,4,5]
list(r)   # []
exhausted
Materialize once
r = list(map(int, "12345"))
r; r
reusable
3. Multi-iterable form stops at the SHORTEST
Unlike zip_longest, map with multiple iterables gives up at the shortest input. Extra items in longer iterables are silently dropped.
Silent drop
list(map(min, [1,2,3], [4,5]))
[1, 2] # third element dropped
itertools.zip_longest
from itertools import zip_longest
list(map(lambda p: min(*p), zip_longest([1,2,3], [4,5], fillvalue=999)))
[1, 2, 3]
4. A comprehension usually reads better than map+lambda
map(lambda x: expr, xs) is functionally identical to [expr for x in xs] but the comprehension is more Pythonic. Reach for map when the callable is already named.
map + lambda
list(map(lambda x: x**2, xs))
works, but stiff
Comprehension
[x**2 for x in xs]
idiomatic

When to use

Use it
  • Applying a NAMED function to an iterable — `map(int, ...)`, `map(str.upper, ...)`
  • Walking two or more iterables in parallel with a binary function
  • Lazy pipelines where you do not want to materialize intermediate lists
  • Interop with functional-style libraries expecting iterators
Reach for something else
  • `map(lambda ...` — use a comprehension instead
  • Need to iterate multiple times → wrap in list()
  • Need &quot;stop at longest&quot; semantics → itertools.zip_longest first
  • Need to modify in place — use a for-loop

Notes

Complexity
O(1) to construct; O(n) to iterate; per-item cost is func()
Return
A map iterator — lazy
CPython impl
Python/bltinmodule.c :: builtin_map
Memory
O(1) — no intermediate list is built
Thread-safe
Depends on func and the underlying iterables

FAQ

Behaviorally almost identical, but map is LAZY (returns an iterator) while a list comprehension is EAGER (returns a list). For a named function, map is compact. For an expression, the comprehension reads better.

History

1.0
map() has been a builtin since Python 1.0 — returned a list.
3.0
Return type changed from list to lazy iterator.