Returns a map object — wrap it in list() to get a list.
What is map()?
map() applies a function to every element of a list and returns a new list of results.
numbers = [1, 2, 3, 4, 5]
# Old way
doubled = []
for num in numbers:
doubled.append(num * 2)
# With map()
doubled = list(map(lambda x: x * 2, numbers))
print(doubled) # [2, 4, 6, 8, 10]
Syntax
map(function, iterable)
Returns a map object — wrap it in list() to get a list.
Basic Examples
Multiplication and Exponentiation
numbers = [1, 2, 3, 4, 5]
doubled = list(map(lambda x: x * 2, numbers))
print(doubled) # [2, 4, 6, 8, 10]
squares = list(map(lambda x: x ** 2, numbers))
print(squares) # [1, 4, 9, 16, 25]
Type Conversion
# str → int
strings = ["1", "2", "3", "4", "5"]
numbers = list(map(int, strings))
print(numbers) # [1, 2, 3, 4, 5]
# int → str
numbers = [10, 20, 30]
strings = list(map(str, numbers))
print(strings) # ['10', '20', '30']
# float → int (drops the decimal part)
floats = [1.5, 2.7, 3.9]
integers = list(map(int, floats))
print(integers) # [1, 2, 3]
Strings
words = ["python", "javascript", "go"]
# Uppercase (no lambda — pass the function directly)
upper_words = list(map(str.upper, words))
print(upper_words) # ['PYTHON', 'JAVASCRIPT', 'GO']
# Word lengths
lengths = list(map(len, words))
print(lengths) # [6, 10, 2]
# Capitalization
names = ["alice", "bob", "carl"]
capitalized = list(map(str.capitalize, names))
print(capitalized) # ['Alice', 'Bob', 'Carl']
map() with Multiple Lists
map() can take multiple iterables — the function receives one element from each:
numbers1 = [1, 2, 3]
numbers2 = [10, 20, 30]
sums = list(map(lambda x, y: x + y, numbers1, numbers2))
print(sums) # [11, 22, 33]
products = list(map(lambda x, y: x * y, numbers1, numbers2))
print(products) # [10, 40, 90]
If the lists differ in length, map() stops at the shortest:
short = [1, 2]
long = [10, 20, 30, 40]
result = list(map(lambda x, y: x + y, short, long))
print(result) # [11, 22] ← only 2 elements
Practical Examples
Processing Prices
prices = [100, 200, 150, 300]
# 20% discount
discounted = list(map(lambda p: round(p * 0.8, 2), prices))
print(discounted) # [80.0, 160.0, 120.0, 240.0]
# 10% tax
with_tax = list(map(lambda p: round(p * 1.1, 2), prices))
print(with_tax) # [110.0, 220.0, 165.0, 330.0]
Working with Dicts
students = [
{"name": "Alice", "grade": 95},
{"name": "Bob", "grade": 87},
{"name": "Carl", "grade": 92}
]
# Extract names
names = list(map(lambda s: s["name"], students))
print(names) # ['Alice', 'Bob', 'Carl']
# Add status
with_status = list(map(
lambda s: {**s, "status": "Excellent" if s["grade"] >= 90 else "Good"},
students
))
# [{'name': 'Alice', 'grade': 95, 'status': 'Excellent'}, ...]
map() + filter()
numbers = [1, 2, 3, 4, 5, 6]
# Double the even numbers
result = list(map(lambda x: x * 2, filter(lambda x: x % 2 == 0, numbers)))
print(result) # [4, 8, 12]
# map + reduce: double → sum
from functools import reduce
doubled = map(lambda x: x * 2, numbers)
total = reduce(lambda acc, x: acc + x, doubled, 0)
print(total) # 42
map() vs List Comprehension
numbers = [1, 2, 3, 4, 5]
doubled_map = list(map(lambda x: x * 2, numbers))
doubled_comp = [x * 2 for x in numbers]
print(doubled_map == doubled_comp) # True
map() is convenient when a ready-made function already exists:
numbers = ["1", "2", "3"]
integers = list(map(int, numbers)) # Cleaner than [int(x) for x in numbers]
Comprehension is better when you need a condition or complex logic:
even_doubled = [x * 2 for x in numbers if x % 2 == 0]
Common Mistakes
Mistake 1: Forgot list()
numbers = [1, 2, 3]
result = map(lambda x: x * 2, numbers)
print(result) # <map object at 0x...> — not a list!
# ✅ CORRECT
result = list(map(lambda x: x * 2, numbers))
print(result) # [2, 4, 6]
Mistake 2: Function Doesn’t Return a Value
def double_bad(x):
x * 2 # Forgot return!
result = list(map(double_bad, [1, 2, 3]))
print(result) # [None, None, None]
# ✅ CORRECT
def double_good(x):
return x * 2
result = list(map(double_good, [1, 2, 3]))
print(result) # [2, 4, 6]
💬 Comments (0)
No comments yet
Be the first to share your opinion about this article!