Lambda Forms and List Comprehensions
The central idea is simple: a lambda packages one parameter and one expression into a function object.
Try it: Functions as Values: Lambda
That a lambda (like def) evaluates to a function object that a name can refer to and that can be passed to other functions — apply(f, x), twice(f, x), map(f, xs), sorted(xs, key=f) — and that each call is traced by binding the argument, substituting it into the single expression and evaluating in operator order.
How it works
- lambda x: expression evaluates to a function object (named <lambda>); nothing inside it is computed yet. f = ... gives it a name, exactly as def f(x): return expression would.
- Writing f without parentheses passes the function object itself; inside apply or twice the parameter func refers to the same object.
- A call binds the argument to the parameter x, substitutes it into the expression, and evaluates by operator precedence (* and % before + and -, parentheses first).
- The value of the expression is the call's return value; there is no other body.
- map(f, xs) calls f once per item and keeps the results in order; sorted(xs, key=f) calls f once per item to get keys, orders by the keys (ties keep their order) and returns the items.
Default run (9 steps): The program is ready: a lambda written inline, then f(x). … print shows 7.
Simplified: Four one-parameter lambdas with editable integer constants, small int arguments (at most 6 items). Each substituted expression the lab shows was evaluated by CPython 3.12 and has the same value as the real call; printed results and ZeroDivisionError messages match CPython.
Loading the simulation…
A Function Without a Name
A lambda statement creates an anonymous function object. Anonymous means that the function is created without giving it a regular function name at the point where it is written. The function can still be called, because evaluating the lambda produces a callable function object.
The central idea is simple: a lambda packages one parameter and one expression into a function object.
The Lambda Pattern
The basic pattern is lambda parameter : expression. The keyword lambda begins the statement, the parameter receives the argument supplied during a call, the colon separates the parameter from the expression, and the expression is the entire function body and its return value.
A lambda has one expression rather than a separate multi-statement body. When the lambda is called, that expression is evaluated and its result is returned automatically. The expression is therefore both the function's work and its return value.
Substitution Before Evaluation
To trace a lambda call, use a consistent three-step process. First, take the argument supplied to the call. Second, bind that value to the lambda's parameter. Third, substitute the value into the expression and evaluate it. The resulting value is returned immediately.
Tracing x * 2 + 1
Determine the result when the lambda expression x * 2 + 1 is called with the argument 4.
- 1Bind the argument
The argument 4 becomes the value of the parameter x.
- 2Substitute the value
Replace x in the expression with 4, producing 4 * 2 + 1.
- 3Follow operation order
Multiplication happens before addition, so calculate 4 * 2 first and then add 1.
- 4Return the expression result
The evaluated expression produces 9, which is returned by the lambda.
The result is 9.
What do you think happens?
Suppose the same lambda expression x * 2 + 1 is called with 3. What result should you expect?
Reveal answer
7
Substitute 3 for x, giving 3 * 2 + 1. Multiplication happens before addition, so the result is 6 + 1, or 7.
Choosing Lambda or def
| Lambda | Regular function with def |
|---|---|
| Anonymous | Has a name |
| Inline | Defined separately |
| Limited to one expression | Can span multiple lines |
| Useful for simple, one-time functions | Useful for reusable functions with complex logic |
The source distinguishes lambda and regular functions by scope, syntax, and use case.
Use a lambda when a short function is needed only in one place, especially when it is passed directly as an argument. A practical use is the key parameter in sort(). The key parameter tells sort() which value to use for comparison, so a simple lambda can provide that rule inline instead of requiring a separate function.
Use a regular function defined with def when the function should have a name, be reused, contain multiple statements, or express complex logic. Although lambda and def create callable function objects for the same general purpose, the regular function is clearer when the logic needs more room or explanation.
Mistakes in Lambda Tracing
Common mistake Treating the expression as only part of the function body
What it looks likeAssuming that a lambda can continue with several separate statements after its expression
Why it failsThe expression after the colon is the entire function body and the value returned by the lambda.
Do this insteadCheck that the lambda's work can be represented by one expression. Use def when multiple statements or complex logic are needed.
Common mistake Skipping parameter binding
What it looks likeTrying to evaluate x * 2 + 1 without first replacing x with the supplied argument
Why it failsThe parameter receives the argument before the expression can be evaluated.
Do this insteadWrite the argument-to-parameter binding explicitly, then substitute the value into the expression.
Common mistake Ignoring operation order
What it looks likeReading x * 2 + 1 as if it were x * (2 + 1)Why it failsMultiplication happens before addition, so the two expressions do not represent the same calculation.
Do this insteadSubstitute the argument first and then follow the order of operations in the resulting expression.
Common mistake Using lambda for logic that needs a named, reusable function
What it looks likeWriting a complicated one-line lambda when the operation needs several statements
Why it failsLambda is intended for simple, one-time functions, while def supports named functions that can span multiple lines and contain complex logic.
Do this insteadPrefer a regular def function when reuse, multiple statements, or clearer explanation is important.
Practice the Trace
Trace a lambda whose parameter is x and whose expression is x * 2 + 1 when it receives the argument 6. Write the parameter binding, the substituted expression, and the returned result.
Hints
- First bind x to 6.
- Then replace x in the expression with 6.
- Evaluate multiplication before addition.
Decide whether lambda or def is clearer for a function that will be used once as the key parameter in sort(). Explain your choice using the ideas of inline use, simplicity, and reuse.
Hints
- A key function can be supplied directly as an argument.
- Consider whether the function is simple and needed only once.
- Compare that situation with a reusable function containing multiple statements.
Lambda Essentials
- 1A lambda statement creates an anonymous function object.
- 2Its structure is lambda, a parameter, a colon, and one expression.
- 3The expression is the entire function body and is returned automatically.
- 4To trace a call, bind the argument to the parameter, substitute it into the expression, and evaluate the result.
- 5Use lambda for simple, one-time inline functions, and use def for named, reusable functions with multiple statements or complex logic.
Key Takeaways
- A lambda creates an anonymous callable function object.
- The lambda pattern contains a parameter, a colon, and a single expression.
- Calling a lambda means binding the argument to the parameter and evaluating the expression.
- Lambda is best for short, one-time functions passed inline, such as a key function for
sort(). - A regular def function is clearer for named, reusable, multi-statement, or complex logic.