map()
map(function, iterable, *iterables) Returns:
iterator · Updated March 13, 2026 · Built-in Functions built-in iteration transformation functional
The map() function takes a function and one or more iterables, applies the function to each item, and returns an iterator with the results. It’s a functional programming tool that transforms data without explicit loops.
Syntax
map(function, iterable, *iterables)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
function | callable | — | A function to apply to each item. Can be any callable (function, lambda, class with __call__). |
iterable | iterable | — | The first iterable to process. |
*iterables | iterables | — | Additional iterables to pass to the function. The function must accept as many arguments as there are iterables. |
Examples
Basic usage with a function
def square(x):
return x ** 2
numbers = [1, 2, 3, 4, 5]
squared = map(square, numbers)
print(list(squared))
# [1, 4, 9, 16, 25]
Using lambda
numbers = [1, 2, 3, 4, 5]
cubed = map(lambda x: x ** 3, numbers)
print(list(cubed))
# [1, 8, 27, 64, 125]
With multiple iterables
a = [1, 2, 3]
b = [10, 20, 30]
added = map(lambda x, y: x + y, a, b)
print(list(added))
# [11, 22, 33]
Combining with strings
words = ["hello", "world", "python"]
uppercased = map(str.upper, words)
print(list(uppercased))
# ['HELLO', 'WORLD', 'PYTHON']
# Using capitalize
capitalized = map(str.capitalize, words)
print(list(capitalized))
# ['Hello', 'World', 'Python']
With type conversion
strings = ["1", "2", "3", "4"]
numbers = map(int, strings)
print(list(numbers))
# [1, 2, 3, 4]
# Float conversion
string_floats = ["1.1", "2.2", "3.3"]
floats = map(float, string_floats)
print(list(floats))
# [1.1, 2.2, 3.3]
Common Patterns
Data transformation pipeline
# Clean and normalize data
raw_data = [" Alice ", "BOB", "charlie "]
cleaned = map(str.strip, map(str.lower, raw_data))
print(list(cleaned))
# ['alice', 'bob', 'charlie']
Using with built-in functions
import math
numbers = [1, 4, 9, 16, 25]
roots = map(math.sqrt, numbers)
print(list(roots))
# [1.0, 2.0, 3.0, 4.0, 5.0]
Object attribute extraction
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
people = [Person("Alice", 30), Person("Bob", 25), Person("Charlie", 35)]
names = map(lambda p: p.name, people)
print(list(names))
# ['Alice', 'Bob', 'Charlie']
Efficient memory usage
map() returns an iterator, not a list. This is memory-efficient for large datasets:
# Don't do this for large files - loads everything into memory
# lines = [line for line in open('large_file.txt')]
# Use map instead - processes one line at a time
def process_line(line):
return line.strip().upper()
# This doesn't load the whole file into memory
result = map(process_line, open('large_file.txt'))
When to Use map()
- Data transformation: Converting one collection type to another
- Applying functions: Running the same function on every element
- Type conversion: Converting strings to numbers, etc.
- String operations: Applying string methods to collections
When NOT to Use map()
- With lambda that does complex logic — use a regular for loop instead for readability
- When you need to filter — use
filter()or list comprehension with condition - When the function has side effects — use a for loop for clarity
- Simple transformations — list comprehensions are often more readable:
[x**2 for x in numbers]
See Also
-
built-in::filter — filter items based on a condition
-
built-in::zip — iterate over multiple iterables in parallel
-
List comprehensions — alternative syntax for simple transformations