Python Reference Library →

Python map() Function – Apply a Function to Each Element

Let’s look at map() function – apply a function to each element

  1. Notice

    map() creates an iterator applying a function to successive items. The function runs as results are requested.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
numbers = range(3)
doubled = map(lambda number: number * 2, numbers)
print(list(doubled))
Live state

Top-level scope

numbers→Object 1
doubled→Object 2
Object 1 · rangerange(0, 3)Object 2 · map<map object>
This step

map() creates an iterator applying a function to successive items. The function runs as results are requested.

Picture map() function – apply a function to each element

map() creates an iterator applying a function to successive items. The function runs as results are requested.

Image brief

Alt text
A diagram for Python map() Function – Apply a Function to Each Element. Instructions: numbers = range(3) doubled = map(lambda number: number * 2, numbers) print(list(doubled)). Displayed result: [0, 2, 4]. The final example bindings are numbers --> object 1 (range); doubled --> object 2 (map). object 1: range(0, 3); object 2: <map object>
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python map() Function – Apply a Function to Each Element". Teaching focus: map() creates an iterator applying a function to successive items. The function runs as results are requested. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS numbers = range(3) doubled = map(lambda number: number * 2, numbers) print(list(doubled)) | v DISPLAYED RESULT [0, 2, 4] FINAL BINDINGS numbers --> object 1 (range) doubled --> object 2 (map) object 1: range(0, 3) object 2: <map object> Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python map() Function – Apply a Function to Each Element. Instructions: numbers = range(3) doubled = map(lambda number: number * 2, numbers) print(list(doubled)). Displayed result: [0, 2, 4]. The final example bindings are numbers --> object 1 (range); doubled --> object 2 (map). object 1: range(0, 3); object 2: <map object>

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
numbers = range(3)
doubled = map(lambda number: number * 2, numbers)
print(list(doubled))
Live state

Top-level scope

numbers→Object 1
doubled→Object 2
Object 1 · rangerange(0, 3)Object 2 · map<map object>
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
[0, 2, 4]

What do you expect this program to print?

What does this example display?

numbers = range(3)
doubled = map(lambda number: number * 2, numbers)
print(list(doubled))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

[0, 2, 4]

Follow the printed values in order. The output is: [0, 2, 4]

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

map() creates an iterator applying a function to successive items. The function runs as results are requested.

map() creates an iterator applying a function to successive items. The function runs as results are requested.

More examples and reference

Python map() Function

The map() function lets you apply a function to each item in a list, tuple, or any other iterable — all in one line. It's a very handy tool for data transformation and functional programming.

Syntax

map(function, iterable)

Parameters:

  • function – A function to apply to each element of the iterable.
  • iterable – A sequence (like a list, tuple, or string) whose items you want to process.

Returns:

  • A map object (an iterator), which you can convert to a list or loop through.

Example: Square Numbers in a List

numbers = [1, 2, 3, 4]
squared = map(lambda x: x * x, numbers)
print(list(squared))
[1, 4, 9, 16]

Example: Convert Strings to Uppercase

words = ["apple", "banana", "cherry"]
uppercase_words = map(str.upper, words)
print(list(uppercase_words))
['APPLE', 'BANANA', 'CHERRY']

Use Case: Clean and Transform Data

Imagine you have a list of prices as strings and want to convert them to floats:

prices = ["10.5", "20.99", "5.00"]
prices_float = map(float, prices)
print(list(prices_float))
[10.5, 20.99, 5.0]

Using map() with Multiple Iterables

You can also use map() with two or more iterables:

a = [1, 2, 3]
b = [4, 5, 6]
sum_values = map(lambda x, y: x + y, a, b)
print(list(sum_values))
[5, 7, 9]

Common Mistakes

  • Forgetting to convert the map object: map() returns an iterator, not a list by default.
  • Not using a function: You must pass a valid function or lambda.
  • Mismatch in length: If using multiple iterables, map() stops at the shortest one.

Interview Tip

map() is often used to clean or transform datasets in one line. It shows functional programming skills and saves you from writing verbose for loops.

Summary

  • map() applies a function to each item of an iterable.
  • Returns a map object (an iterator).
  • Often used with lambda for quick transformations.
  • Works with multiple iterables too.

Practice Problem

Write a program that takes a list of numbers and returns a new list with the cube of each number using map().

nums = [2, 3, 4]
cubes = map(lambda x: x**3, nums)
print(list(cubes))

What to remember

Connect what you noticed in the example with the idea below.

Remember

map() creates an iterator applying a function to successive items. The function runs as results are requested.

Watch out for this idea

map() always changes the original iterable.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →