iteration
map()
Applies a function to each item of one or more iterables, yielding results lazily. Returns a map object (iterator), not a list.
map(function, *iterables) Parameters
| Parameter | Purpose |
|---|---|
| function | Callable applied to each element |
| *iterables | One or more iterables — function gets one arg per iterable |
Examples
list(map(str.upper, ['a', 'b'])) Returns ['A', 'B']
list(map(lambda x, y: x + y, [1, 2], [10, 20])) Returns [11, 22]
list(map(int, ['1', '2', '3'])) Returns [1, 2, 3]
Gotcha
map() returns an iterator — it's exhausted after one pass, and printing it shows '
Related built-ins
filter()
Yields items from iterable for which function(item) is truthy. If function is None, filters out falsy items.
zip()
Pairs elements from multiple iterables into tuples, stopping at the shortest. With strict=True, raises ValueError if lengths differ.
sorted()
Returns a new sorted list from any iterable. Uses Timsort — stable, O(n log n).
enumerate()
Yields (index, item) tuples from an iterable. Preferred over manual index counters in for-loops.
range()
Returns an immutable range object representing an arithmetic sequence of integers. Memory-efficient — values are computed lazily, not stored.