Remember
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.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
numbers = range(3)
doubled = map(lambda number: number * 2, numbers)
print(list(doubled))Top-level scope
range(0, 3)Object 2 · map<map object>map() creates an iterator applying a function to successive items. The function runs as results are requested.
(no output)
What do you expect this program to print?
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>Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
numbers = range(3)
doubled = map(lambda number: number * 2, numbers)
print(list(doubled))Top-level scope
range(0, 3)Object 2 · map<map object>The example has finished. Compare the final output with the values and references above.
[0, 2, 4]
What do you expect this program to print?
numbers = range(3)
doubled = map(lambda number: number * 2, numbers)
print(list(doubled))map() FunctionThe 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.
map(function, iterable)
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.map object (an iterator), which you can convert to a list or loop through.numbers = [1, 2, 3, 4]
squared = map(lambda x: x * x, numbers)
print(list(squared))
[1, 4, 9, 16]
words = ["apple", "banana", "cherry"]
uppercase_words = map(str.upper, words)
print(list(uppercase_words))
['APPLE', 'BANANA', 'CHERRY']
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]
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]
map() returns an iterator, not a list by default.map() stops at the shortest one.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.
map() applies a function to each item of an iterable.map object (an iterator).lambda for quick transformations.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))
Connect what you noticed in the example with the idea below.
map() creates an iterator applying a function to successive items. The function runs as results are requested.
map() always changes the original iterable.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.