Lambda Forms and List Comprehensions
A lambda statement creates an anonymous function object with one parameter and one expression.
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.
Bind the argument: The argument 4 becomes the value of the parameter x.
Substitute the value: Replace x in the expression with 4, producing 4 * 2 + 1.
Follow operation order: Multiplication happens before addition, so calculate 4 * 2 first and then add 1.
Return 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
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 |
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
Treating the expression as only part of the function body
The expression after the colon is the entire function body and the value returned by the lambda.
Fix:
Check that the lambda's work can be represented by one expression. Use def when multiple statements or complex logic are needed.Skipping parameter binding
The parameter receives the argument before the expression can be evaluated.
Fix:
Write the argument-to-parameter binding explicitly, then substitute the value into the expression.Ignoring operation order
Multiplication happens before addition, so the two expressions do not represent the same calculation.
Fix:
Substitute the argument first and then follow the order of operations in the resulting expression.Using lambda for logic that needs a named, reusable function
Lambda is intended for simple, one-time functions, while def supports named functions that can span multiple lines and contain complex logic.
Fix:
Prefer 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
- A lambda statement creates an anonymous function object.
- Its structure is lambda, a parameter, a colon, and one expression.
- The expression is the entire function body and is returned automatically.
- To trace a call, bind the argument to the parameter, substitute it into the expression, and evaluate the result.
- Use 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.