Last modified: Nov 19, 2026

Python map Function Explained

The map function is one of the most useful built-ins in Python. It applies a function to every item in an iterable.

This article explains how map works. It covers syntax, examples, and common mistakes. Beginners will find it easy to follow.

What Is the Python map Function?

map takes a function and one or more iterables. It returns a map object. That object produces a new value for each item.

Think of it as a fast way to transform a list. Instead of writing a loop, you pass a function and the data.

The result is lazy. Values are computed only when you ask for them. This saves memory on large data sets.

Basic Syntax of map

The syntax is simple and short.


map(function, iterable, ...)

The function is applied to each item. The iterable can be a list, tuple, string, or any iterable.

You can pass more than one iterable. In that case, the function must accept the same number of arguments.

Example 1: Square Every Number

Let's start with a simple example. We square each number in a list.


numbers = [1, 2, 3, 4, 5]

# Apply a function to each item
squared = map(lambda x: x ** 2, numbers)

# Convert the map object to a list
print(list(squared))

[1, 4, 9, 16, 25]

The lambda function runs once for each number. The map object holds the results. We wrap it in list to see them.

If you are new to lambdas, read this guide on the Python lambda function syntax and examples first.

Example 2: Use a Named Function

You do not have to use a lambda. A regular function works just as well.


def double(x):
    return x * 2

numbers = [10, 20, 30]

# Pass a named function to map
result = map(double, numbers)

print(list(result))

[20, 40, 60]

Named functions are easier to read. They are also easier to test. Use them when the logic is more than one line.

Example 3: map with Two Iterables

map can take more than one iterable. It pairs items by position.


a = [1, 2, 3]
b = [10, 20, 30]

# Add items from both lists
sums = map(lambda x, y: x + y, a, b)

print(list(sums))

[11, 22, 33]

This works like a zipper. Each item from the first list meets an item from the second. This idea is covered in more detail in this guide on the Python zip function to combine iterables.

If the lists have different lengths, map stops at the shortest one. Keep that in mind to avoid surprises.

Example 4: map with Strings

Strings are iterables too. You can transform each character.


word = "python"

# Uppercase each character
upper = map(str.upper, word)

print(list(upper))

['P', 'Y', 'T', 'H', 'O', 'N']

Notice that we passed str.upper directly. No lambda was needed. This is a clean and fast pattern.

map vs a For Loop

A for loop can do the same job. So why use map?

map is shorter. It expresses intent in one line. It also avoids an empty result list and manual appends.

map is lazy. It does not build the whole result at once. This helps with big data.

But a loop is often clearer for complex logic. Pick the tool that fits the task.

map Returns an Iterator

This is a common beginner mistake. In Python 3, map returns an iterator, not a list.


numbers = [1, 2, 3]
result = map(lambda x: x + 1, numbers)

print(result)

You must convert it to see the values. Use list, tuple, or set.


print(list(result))

[2, 3, 4]

map Can Only Be Used Once

Because map returns an iterator, it is exhausted after one pass. A second loop over it gives nothing.


numbers = [1, 2, 3]
result = map(lambda x: x * 10, numbers)

print(list(result))  # first use
print(list(result))  # second use: empty

[10, 20, 30]
[]

Fix this by storing the list. Or create a new map object each time.

Combine map with enumerate

Sometimes you need the index and the value. You can pair map with other tools.


names = ["Ann", "Bob", "Cara"]

# Build labels with positions
labels = map(lambda item: f"{item[0]}: {item[1]}", enumerate(names, start=1))

print(list(labels))

['1: Ann', '2: Bob', '3: Cara']

For more patterns like this, see this complete guide to Python enumerate.

map with Built-in Functions

map pairs well with built-ins. They keep your code short and fast.


strings = ["1", "2", "3"]

# Convert strings to integers
numbers = map(int, strings)

print(list(numbers))

[1, 2, 3]

Other good choices include len, abs, and round. Use them directly and skip the lambda.

map vs List Comprehension

List comprehensions are another way to transform data. Many Python developers prefer them.


numbers = [1, 2, 3, 4]

# map version
mapped = list(map(lambda x: x ** 2, numbers))

# comprehension version
squared = [x ** 2 for x in numbers]

print(mapped)
print(squared)

[1, 4, 9, 16]
[1, 4, 9, 16]

Both give the same result. A comprehension is often more readable. map shines when you already have a function ready.

As a rule of thumb, use map with named or built-in functions. Use a comprehension for simple inline logic.

Common Mistakes to Avoid

Forgetting to convert the result. Always wrap the map object when you need a list.

Reusing an exhausted iterator. Store the output if you need it twice.

Passing mismatched arguments. With two iterables, the function must take two parameters.

Using map for side effects.map is for building new values. Use a loop to print or mutate.

When Should You Use map?

Use map when you have a function and an iterable. It is clean and fast.

It works well with built-ins like int and str.strip. It also fits data pipelines with other iterators.

Skip it when the logic is complex. A loop or comprehension may be clearer for readers.

Quick Reference Example

Here is a full example that ties it all together.


prices = [100, 250, 80, 400]

# Apply a 10% discount to each price
discounted = map(lambda p: round(p * 0.9, 2), prices)

# Print the final list
print(list(discounted))

[90.0, 225.0, 72.0, 360.0]

The code is short and easy to read. It shows the core power of map.

Conclusion

The Python map function applies a function to every item in an iterable. It returns a lazy iterator, not a list.

Use it with lambdas, named functions, or built-ins. Combine it with multiple iterables when you need to pair data.

Remember the key rules. Convert the result when needed. Do not reuse an exhausted iterator. Choose the clearest tool for the job.

With these examples, you can now use map with confidence in your own projects.