04.01 · Transform and filter values / Learn · 9 min
Write a small function as one expression
A lambda receives arguments and returns the value of one expression.
Activity 1 of 6 in Transform and filter values
Python 3: Lists / 04.01
Understand lambda
Not started
Read a lambda expression, compare it with def and use a simple sorting key.
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.

Arguments go before the colon; the expression follows
lambda first, second: first + second creates a function with two parameters. Calling it with 3 and 5 evaluates the expression and returns 8. There is no return keyword inside the expression.
The source lesson is text-only, so there is no video to unlock here. This example gives the function a name so you can call and inspect it. For an ordinary reusable named function, def is usually clearer.
add = lambda first, second: first + second
print(add(3, 5)) # 8
def add_with_def(first, second):
return first + second
print(add_with_def(3, 5)) # 8Use a small expression where another function is expected
A sorting key function receives one item and returns the value used for comparison. Here each record is a two-item list: a name and a score. record[1] selects its score. The original records are retained in the sorted result.
The source also previews tuple and dictionary records. These examples use lists so you can concentrate on the function without needing a new collection type. A lambda is not automatically faster or better than a named function.
records = [["Ada", 80], ["Rob", 60], ["Grace", 90]]
by_score = sorted(records, key=lambda record: record[1])
print(by_score)
print(records) # original orderKeep it readable
A lambda body is one expression, not a block of statements. Use def when the task needs several statements, a clearer name, comments or more explanation.
The next practice uses a named square lambda to make the returned values easy to inspect. Then map and filter will show where passing a short function directly can be useful.
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)3 steps · Saved work · Instant feedback
YOUR TURN
Return the square, not the double
Change square to a one-expression square function. Keep its calls for 7 and -3 and print their stored results, 49 then 9.
49 9
Keep the starting inputs. Print these lines in this order, without extra output.
- Store the requested seven result
- Store the requested negative result
- Output 1 matches the requested result
- Output 2 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?
Square is not double
Multiply number by itself or raise it to the power 2. Let the expression use its argument.
One possible solution
Your code may look different and still be correct. Compare the approach with your own.
Each call calculates from its own argument. The expression’s value is returned without a return statement inside the lambda.
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.
square = lambda number: number ** 2Expected output for this step
(No output)
Apply the function and inspect each result
Each call calculates from its own argument. The expression’s value is returned without a return statement inside the lambda.
square = lambda number: number ** 2
seven_result = square(7)
negative_result = square(-3)
print(seven_result)
print(negative_result)Expected output for this step
49 9
See the complete solution
square = lambda number: number ** 2
seven_result = square(7)
negative_result = square(-3)
print(seven_result)
print(negative_result)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

7 days freeTry The Code Zone
THE CODE ZONE COMMUNITY
Your next step is easier together.
Try the community free for 7 days.
No charge during your trial. Choose your plan on Skool.
- Ask questions and get support in the community
- Watch every course video walkthrough
- Find a study buddy in the 24/7 study room
Make the most of your trial with Premium.
Choose Premium on Skool to try Thursday live help sessions. Bring your questions, work through code, and get unstuck together.
Scheduled Thursdays in Eastern Time. Check the Skool calendar for your local date and time.
Have a website account? Sign in