thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

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

All courses

Python 3: Lists / 04.03

Transform with map

Not started

Apply a callable to each input, collect the iterator and compare multiple inputs.

More options

Your lesson progress

Walkthrough: not startedPractice: not started

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.

Rob maps 1, 2 and 3 through multiplication by 2 to ordered results 2, 4 and 6.
Rob’s comic strip · Transform and filter values

ORIGINAL WALKTHROUGH · 7:34

Watch with Rob.

Pause whenever you want to try a change. You can return to the lesson at any time.

LEARN WITH THE CODE ZONE

Unlock this video walkthrough

Written lessons and coding practice are free. Video walkthroughs are included with a paid Code Zone membership or an active free trial.

Have a Code Zone website account? Use your learning account’s email to get a six-digit sign-in code, or use your existing website password.

Need a website account? Paid Skool members and active trial members can use the email from their membership questions to request a sign-in code. No invite link or Skool password needed.

Close this window to keep practicing. After signing in, you’ll return to this lesson.

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)

Starter source code for VS Code

Copy the source below or download the file to follow along in VS Code.

The download includes a lesson link in each file and a README so you can find your way back.

main.py
main.pypython · example
# Code along with Rob: 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.
# Predict each result, then press play.

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.

Pass the function, not a call to it
collections.pypython · example
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]
Open workspace to paste this example (opens in a new tab)

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.

Use existing callables and short expressions
collections.pypython · example
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))  # []
Open workspace to paste this example (opens in a new tab)

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.

Pair corresponding items from multiple inputs
collections.pypython · example
left = [2, 4, 6]
right = [1, 3]
result = list(map(lambda first, second: first - second, left, right))
print(result)  # [1, 1]
Open workspace to paste this example (opens in a new tab)

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.

Expected output
['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.

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.

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

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)

Continue learning

ONE STEP FORWARD

Map applies a function to each input. Collect its iterator when you need a reusable list.