YOUR SAVED CODE-ALONG
Code along: Transform with map
Keep the lesson open on your other screen. Your edits stay in this workspace, separate from the practice activity. Sign in to save and reload a private account draft across devices, or share a snapshot when you need help.
CODE-ALONG / THEATER MODE
Transform with map
ORIGINAL WALKTHROUGH · 7:34
Watch with Rob.
Pause whenever you want to try a change. You can return to the lesson at any time.
You are signed in, but video access needs attention. Check your account access. Your saved work is kept.
Video walkthroughs are included with an active Code Zone membership. The written lesson and practice are free.
Video settings & progress
Your lesson progress
Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.
Start the video to enable typing controls.
YOUR TURN
Code along: Transform with map
Apply a named function, a built-in and a lambda to each item. Collect map once and observe iterator exhaustion. Pair two inputs and compare their lengths. Pause the original recording to type and run your examples. This open workspace saves separately from scored practice and has no fixed-answer checks.
Load an example, edit it and run it. Word wrap is enabled.
Your check results
Lesson reference
Examples load into this same code-along only after you confirm replacing your code.
Pass the function, not a call to it
map(double, numbers) receives the function itself. It calls double for each value as the iterator is consumed. Writing double() there would call the function immediately without its required argument.
In Python 3, map returns an iterator, not a list. list(...) consumes it into a list you can print, index and reuse. This version leaves the original numbers unchanged.
def double(number):
return number * 2
numbers = [1, 2, 3]
doubled = list(map(double, numbers))
print(doubled) # [2, 4, 6]
print(numbers) # [1, 2, 3]Use existing callables and short expressions
You can pass str.upper, len or abs directly. A lambda is useful when the operation needs a small expression. Choose whichever makes the transformation clear to a reader.
An iterator is consumed as you take values from it. Converting the same map object a second time gives an empty list after the first conversion used all its items. Keep the materialized list if you need to reuse the results.
words = ["hello", "world", "python"]
print(list(map(str.upper, words)))
print(list(map(len, words)))
iterator = map(lambda number: number ** 2, [1, 2, 3])
print(list(iterator)) # [1, 4, 9]
print(list(iterator)) # []Pair corresponding items from multiple inputs
A two-parameter function can receive one item from each of two iterables. This example subtracts right from left at each matching position.
By default, map stops when the shortest input is exhausted. It does not create all combinations. For these small examples a loop can be equally clear; avoid assuming that map is always faster or more readable.
left = [2, 4, 6]
right = [1, 3]
result = list(map(lambda first, second: first - second, left, right))
print(result) # [1, 1]Build it one step at a time
Prefer learning by typing? Work through short instructions, run the Python code and check each change before continuing. Your guided project saves separately from the challenge below.
The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.
Start mini-lesson(opens in a new tab)4 steps · Saved work · Instant feedback
See the attached lesson’s comic
