Python map() function applies a function to every element of an iterable (like a list, tuple, or set) and returns a map object (an iterator).
# Example numbers = [1, 2, 3, 4] result = list(map(lambda x: x * 2, numbers)) print(result)
Output:
[2, 4, 6, 8]
# using a named function def square(n): return n * n numbers = [1, 2, 3, 4, 5] # Apply square() to each element result = map(square, numbers) # Convert map object to list print(list(result))
Output:
[1, 4, 9, 16, 25]
Using map() with a lambda (inline)
numbers = [1, 2, 3, 4, 5] # Double each number using lambda result = list(map(lambda x: x * 2, numbers)) print(result)
Output:
[2, 4, 6, 8, 10]
Using `map()` with multiple iterables
numbers1 = [1, 2, 3] numbers2 = [4, 5, 6] # Add corresponding elements from two lists result = list(map(lambda x, y: x + y, numbers1, numbers2)) print(result)
Output:
[5, 7, 9]
Type conversion: strings → ints
str_nums = ['10', '20', '30', '40'] # Convert strings to integers int_nums = list(map(int, str_nums)) print(int_nums)
Output:
[10, 20, 30, 40]
Using `map()` with tuples
nums_tuple = (2, 4, 6, 8) # Cube each number, return a tuple cubed = tuple(map(lambda x: x ** 3, nums_tuple)) print(cubed)
Output:
(8, 64, 216, 512)
Conditional transformation inside `map()`
numbers = [5, 10, 15, 20, 25] # Replace numbers greater than 10 with the string "Big" result = list(map(lambda x: "Big" if x > 10 else x, numbers)) print(result)
Output:
[5, 10, 'Big', 'Big', 'Big']
Cleaning strings with a user-defined function
def format_name(name): return name.strip().title() names = [' alice ', ' BOB', 'charlie '] clean_names = list(map(format_name, names)) print(clean_names)
Output:
['Alice', 'Bob', 'Charlie']
Processing list of dicts
people = [
{'name': 'Alice', 'age': 25},
{'name': 'Bob', 'age': 30},
{'name': 'Charlie', 'age': 35}
]
# Extract names
names = list(map(lambda p: p['name'], people))
print(names)
Output:
['Alice', 'Bob', 'Charlie']
Combine `map()`, `filter()`, and `reduce()`
from functools import reduce numbers = [1, 2, 3, 4, 5, 6] # Double, filter those divisible by 4, then sum mapped = map(lambda x: x * 2, numbers) filtered = filter(lambda x: x % 4 == 0, mapped) result = reduce(lambda a, b: a + b, filtered) print(result)
Output:
12
Performance of map() function
map() returns an iterator (lazy) which is Good for memory efficiency.
mapped_iter = map(lambda x: x * 2, range(10**6)) # To materialize: first_ten = list(mapped_iter)[:10]
map() is often more memory-friendly than building large lists immediately.
FAQs — Python map() Function
What is the Python map() function used for?
The map() function in Python applies a given function to all items in an iterable (like a list or tuple) and returns a map object (an iterator).
numbers = [1, 2, 3, 4]
result = map(lambda x: x * 2, numbers)
print(list(result)) # Output: [2, 4, 6, 8]
What is the syntax of the Python map() function?
map(function, iterable, ...)
function→ The function to apply.iterable→ One or more iterables whose elements the function will process.
How to use map() with a user-defined function in Python?
You can define your own function and pass it to map():
def square(n):
return n * n
numbers = [1, 2, 3, 4]
result = map(square, numbers)
print(list(result)) # Output: [1, 4, 9, 16]
Can Python map() function take multiple iterables?
Yes. The function should accept as many arguments as there are iterables:
numbers1 = [1, 2, 3]
numbers2 = [4, 5, 6]
result = map(lambda x, y: x + y, numbers1, numbers2)
print(list(result)) # Output: [5, 7, 9]
What is the return type of the Python map() function?
The map() function returns a map object (an iterator). Convert it to a list, tuple, or set to view the results:
list(result)
Can the Python map() function be used with built-in functions?
Yes. You can use built-in functions like str(), len(), or abs() with map():
words = ['hi', 'python', 'map']
print(list(map(len, words))) # Output: [2, 6, 3]
How is Python map() function different from list comprehension?
Both can achieve similar results, but map() is slightly faster for built-in functions, while list comprehensions are more readable:
# Using map()
list(map(str.upper, ['a', 'b', 'c']))
# Using list comprehension
[s.upper() for s in ['a', 'b', 'c']]
How to use map() with lambda functions in Python?
lambda functions are anonymous inline functions commonly used with map():
nums = [1, 2, 3]
print(list(map(lambda x: x**2, nums))) # Output: [1, 4, 9]
Can I use Python map() function with strings?
Yes, but you must pass an iterable string (or list of strings):
text = ['hello', 'world']
print(list(map(str.upper, text))) # Output: ['HELLO', 'WORLD']
What happens if I pass None as a function to Python map()?
In Python 3, you cannot pass None as the function argument — it will raise a TypeError. You must always specify a function.
Reference: https://docs.python.org/3/library/functions.html#map