04.03 · Transform and filter values / Watch · 14 min
Transform each item with map
Mapping changes each item through a function. It does not choose a subset.
Activity 3 of 6 in Transform and filter values
Python 3: Lists / 04.03
Transform with map
Not started
Apply a callable to each input, collect the iterator and compare multiple inputs.
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.

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.
Open video & code editor (opens in a new tab)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
YOUR TURN
Apply all three source transformations
Use map to produce standard_names, magnitudes and paired differences. Collect actual lists, preserve every input and print the three results in order.
['John', 'Jane', 'Alice'] [5, 3, 1, 9, 2] [1, 1, 1]
Keep the starting inputs. Print these lines in this order, without extra output.
- Store the requested names
- Store the requested signed numbers
- Store the requested left
- Store the requested right
- Store the requested standard names
- Store the requested magnitudes
- Store the requested differences
- Output 1 matches the requested result
- Output 2 matches the requested result
- Output 3 matches the requested result
Edit Python, then run it. Check challenge checks your output and the listed Python requirements. Your code saves on this browser after a short pause. Sign in and choose Save account draft to keep it across devices. Word wrap is enabled.
Ready to checkRun the checks to see your results here.
Your check results
Need a nudge?
Pass a callable
Use str.capitalize and abs without calling them first. Wrap the map results in list.
Pair two arguments
The lambda receives one value from left and one from right; subtract the second from the first.
One possible solution
Your code may look different and still be correct. Compare the approach with your own.
Each source list keeps its values. The transformations produce new lists whose elements follow the chosen callable.
Walk through the fix.
Choose Next to inspect one change at a time. Highlighted lines show what to notice. Your editor stays unchanged unless you choose to load a step.
Step 1 of 2
Read the original inputs
Identify the original values before choosing the transformation or keep rule.
names = ["John", "JANE", "alice"]Expected output for this step
(No output)
Apply the function and inspect each result
Each source list keeps its values. The transformations produce new lists whose elements follow the chosen callable.
names = ["John", "JANE", "alice"]
signed_numbers = [-5, 3, -1, 9, -2]
left = [2, 4, 6]
right = [1, 3, 5]
standard_names = list(map(str.capitalize, names))
magnitudes = list(map(abs, signed_numbers))
differences = list(map(lambda first, second: first - second, left, right))
print(standard_names)
print(magnitudes)
print(differences)Expected output for this step
['John', 'Jane', 'Alice'] [5, 3, 1, 9, 2] [1, 1, 1]
See the complete solution
names = ["John", "JANE", "alice"]
signed_numbers = [-5, 3, -1, 9, -2]
left = [2, 4, 6]
right = [1, 3, 5]
standard_names = list(map(str.capitalize, names))
magnitudes = list(map(abs, signed_numbers))
differences = list(map(lambda first, second: first - second, left, right))
print(standard_names)
print(magnitudes)
print(differences)Ready to try it?
Practice in your browser and see your results.
Progress details
Have a question?
Ask the Code Zone community.
Ask a question in The Code Zone (opens in a new tab)