How to Use Map in Python
What you’ll build or solve
You’ll transform items in an iterable using map() in Python.
When this approach works best
map() works well when you:
Learn Python on Mimo
- Convert a list of strings into numbers, like parsing IDs from a CSV column.
- Apply the same formatting to many strings, like trimming and lowercasing tags.
- Compute a new list from an old one, like turning prices into totals with tax.
Avoid map() when the logic needs multiple steps or side effects like printing. A for loop or a list comprehension is often clearer in those cases.
Prerequisites
- Python installed
- You know what a function is
- You know what a list is
Step-by-step instructions
1) Use map() to apply a function to every item
map(function, iterable) returns an iterator of transformed values.
numbers= ["1","2","10"]
as_ints=map(int,numbers)
print(list(as_ints))
What to look for:
map() returns an iterator in Python 3. Convert it to a list if you want to print the values or reuse them.
2) Use a lambda when you need a small custom transform
A lambda works well for a short, single-expression transform.
prices= [10,25,7]
with_tax=list(map(lambdap:round(p*1.2,2),prices))
print(with_tax)
Option A: Use a named function for reuse
defadd_tax(price):
returnround(price*1.2,2)
prices= [10,25,7]
with_tax=list(map(add_tax,prices))
print(with_tax)
What to look for:
Use a named function when the transform has a meaningful name or you need it in more than one place.
3) Map across multiple iterables
map() can take more than one iterable. Your function must accept the same number of arguments.
qty= [2,1,3]
price= [10,25,7]
totals=list(map(lambdaq,p:q*p,qty,price))
print(totals)
What to look for:
map() stops when the shortest iterable ends. This matters if your inputs can have different lengths.
Examples you can copy
Example 1: Convert user input split by commas into integers
text="10, 20, 30"
parts=text.split(",")
numbers=list(map(lambdas:int(s.strip()),parts))
print(numbers)
Example 2: Normalize tags for matching
tags= [" Python","SQL "," css "]
normalized=list(map(lambdat:t.strip().lower(),tags))
print(normalized)
Example 3: Extract a field from dictionaries
users= [{"id":"u1"}, {"id":"u2"}, {"id":"u3"}]
ids=list(map(lambdau:u["id"],users))
print(ids)
Example 4: Compute totals from two lists
qty= [1,2,1]
price= [9.99,4.50,2.00]
totals=list(map(lambdaq,p:round(q*p,2),qty,price))
print(totals)
Example 5: Apply a method directly without a lambda
names= ["mina","ivan","lea"]
capitalized=list(map(str.capitalize,names))
print(capitalized)
Common mistakes and how to fix them
Mistake 1: Forgetting that map() returns an iterator
What you might do
numbers= ["1","2","3"]
mapped=map(int,numbers)
print(mapped)
Why it breaks
Printing shows a map object, not the values.
Fix
Convert to a list (or loop over it):
numbers= ["1","2","3"]
mapped=list(map(int,numbers))
print(mapped)
Mistake 2: Expecting map() to run twice without recreating it
What you might do
names= [" Naomi ","Ivan "]
cleaned=map(str.strip,names)
print(list(cleaned))
print(list(cleaned))
Why it breaks
The iterator is consumed after the first list(cleaned).
Fix
Convert once and reuse the list:
names= [" Naomi ","Ivan "]
cleaned=list(map(str.strip,names))
print(cleaned)
print(cleaned)
Troubleshooting
If you see <map object at ...>, wrap it with list(...) or loop over it.
If your result is empty the second time, you already consumed the iterator. Recreate the map() call or store a list.
If you get a TypeError about arguments, your function signature does not match the number of iterables passed to map().
If results look cut off, one of your iterables is shorter. map() stops at the shortest length.
If your transform needs multiple steps, switch to a list comprehension or a for loop for readability.
Quick recap
- Use
map(func, items)to apply a function to every item. - Convert the
map()result to a list when you want to print or reuse it. - Use a lambda for short transforms, or a named function for reuse.
- Pass multiple iterables to
map()to combine values pairwise. - Use a loop or list comprehension when the logic is more than a simple transform.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot