Python map() Function Explained
Python’s map() function applies the same function to each item in an iterable.
This is useful when you want to transform data, such as:
- converting numbers to strings
- changing strings to integers
- doubling every value in a list
The main thing beginners need to know is this:
map()does not return a list in Python 3- it returns a map object
- you often convert that result with
list()
Quick example #
numbers = [1, 2, 3, 4]
result = map(str, numbers)
print(list(result))
Output:
['1', '2', '3', '4']
Use map(function, iterable) to apply the same function to each item. Convert the result to list() if you want to see all mapped values at once.
What map() does #
map() applies a function to each item in an iterable.
An iterable is something you can loop over, such as:
- a list
- a tuple
- a string
- a range object
The basic form is:
map(function, iterable)
For each item in the iterable, Python calls the function and produces a new value.
Important: the result is a map object, not a list.
If you want to understand iterables better, see iterators and iterable objects explained.
Basic syntax #
map() needs at least two parts:
- Function: the function to run on each item
- Iterable: the values to process
Basic syntax:
map(function, iterable)
You can pass:
- built-in functions like
str,int, orabs - your own custom function
- a lambda function for short one-line logic
If you are new to lambda functions, see lambda functions in Python explained.
You will often see map() used like this:
list(map(function, values))
That is because list() turns the map object into a normal list you can print or reuse.
Simple example #
Here is a simple example that converts numbers to strings:
numbers = [10, 20, 30]
result = map(str, numbers)
print(list(result))
Output:
['10', '20', '30']
What happens here:
numberscontains integersmap(str, numbers)passes each number tostr()str(10)becomes'10'str(20)becomes'20'str(30)becomes'30'
So map() applies str() one item at a time.
If you want to understand list() better, see Python list() function explained.
Using map() with your own function #
You can also use map() with a function you define yourself.
def double(x):
return x * 2
numbers = [1, 2, 3, 4]
result = map(double, numbers)
print(list(result))
Output:
[2, 4, 6, 8]
How this works:
map()takes the functiondouble- it sends each item from
numbersinto that function - the returned values become the mapped result
In other words, Python does this behind the scenes:
double(1)→2double(2)→4double(3)→6double(4)→8
Using map() with multiple iterables #
map() can also work with more than one iterable.
When you do that, the function must accept the same number of arguments.
Example:
def add_numbers(a, b):
return a + b
list1 = [1, 2, 3]
list2 = [10, 20, 30]
result = map(add_numbers, list1, list2)
print(list(result))
Output:
[11, 22, 33]
Here:
- the first values are used together:
add_numbers(1, 10) - then
add_numbers(2, 20) - then
add_numbers(3, 30)
map() stops at the shortest iterable #
If the iterables have different lengths, map() stops when the shortest one ends.
def add_numbers(a, b):
return a + b
list1 = [1, 2, 3, 4]
list2 = [10, 20]
result = map(add_numbers, list1, list2)
print(list(result))
Output:
[11, 22]
Only two results are produced because list2 has only two items.
map() returns an iterator-like object #
In Python 3, map() returns a map object.
This object is lazy, which means values are produced only when needed.
Why print(result) looks strange #
Beginners often expect this:
numbers = [1, 2, 3]
result = map(str, numbers)
print(result)
Output will look something like this:
<map object at 0x...>
That happens because you are printing the map object itself, not the values inside it.
To see the mapped values, convert it to a list or loop over it:
numbers = [1, 2, 3]
result = map(str, numbers)
print(list(result))
Output:
['1', '2', '3']
You can loop over a map object directly #
numbers = [1, 2, 3]
result = map(str, numbers)
for item in result:
print(item)
Output:
1
2
3
A map object is exhausted after use #
Once you consume a map object, it is empty the next time.
numbers = [1, 2, 3]
result = map(str, numbers)
print(list(result))
print(list(result))
Output:
['1', '2', '3']
[]
The second list is empty because the map object was already used.
If you need the values more than once, store them in a list:
numbers = [1, 2, 3]
result = list(map(str, numbers))
print(result)
print(result)
Output:
['1', '2', '3']
['1', '2', '3']
When to use map() #
map() is a good choice when you want to apply one function to every item.
It works especially well with simple built-in functions such as:
strintabs
Example:
values = ['1', '2', '3']
numbers = list(map(int, values))
print(numbers)
Output:
[1, 2, 3]
For beginners, a list comprehension is often easier to read.
For example, these do similar work:
numbers = [1, 2, 3]
result = list(map(str, numbers))
print(result)
numbers = [1, 2, 3]
result = [str(n) for n in numbers]
print(result)
Both produce:
['1', '2', '3']
A simple rule:
- use
map()when applying a clear function to every item - use a
forloop when you need more steps or more explanation - use a list comprehension when you want a compact and readable transformation
If you want more practice with that style, see how to use list comprehensions in Python.
Common beginner mistakes #
1. Forgetting to convert the result to list() #
This is very common:
numbers = [1, 2, 3]
result = map(str, numbers)
print(result)
This prints the map object, not the values.
Fix:
print(list(result))
2. Calling the function instead of passing it #
Wrong:
numbers = [1, 2, 3]
result = map(str(), numbers)
This is wrong because str() is being called immediately. map() needs the function itself, not the result of calling it.
Correct:
numbers = [1, 2, 3]
result = map(str, numbers)
print(list(result))
3. Using a function with the wrong number of parameters #
If your function needs two arguments, but you pass only one iterable, it will fail.
Wrong:
def add(a, b):
return a + b
numbers = [1, 2, 3]
result = map(add, numbers)
print(list(result))
Correct:
def add(a, b):
return a + b
list1 = [1, 2, 3]
list2 = [10, 20, 30]
result = map(add, list1, list2)
print(list(result))
4. Reusing the same map object #
Wrong:
numbers = [1, 2, 3]
result = map(str, numbers)
print(list(result))
print(list(result))
The second result is empty.
Fix: create a new map object, or convert once and save the list.
Helpful checks while debugging #
If map() is not doing what you expect, these quick checks can help:
print(result)
print(list(result))
print(type(result))
for item in result:
print(item)
What these show:
print(result)shows that you have a map objectprint(list(result))shows the actual mapped valuesprint(type(result))confirms the result type- looping over
resultlets you inspect items one by one
Be careful: list(result) and a for loop consume the map object.
FAQ #
Does map() return a list in Python? #
No. In Python 3, map() returns a map object. Use list(map(...)) if you need a list.
Is map() better than a for loop? #
Not always. map() is shorter for simple transformations, but a for loop can be easier to read.
Can map() use more than one list? #
Yes. Pass multiple iterables, and use a function that accepts one argument for each iterable.
Why is my map object empty the second time? #
A map object is consumed as you iterate over it. Create a new one if you need to use it again.