Module lessons (4/4)
Lambdas and higher-order functions
Anonymous functions with lambda, use with sorted/max/min, when to prefer def, and sorted(..., key=...).
What you'll learn in this lesson
- Write anonymous functions of a single expression (
lambda params: expr). - Pass
lambdaand built-in functions as argumentskey=to higher-order functions (sorted,max,min). - Recognize when
defand explicit closures offer superior readability and debugging thanlambda.
Overview
A lambda is an anonymous inline function consisting of a single expression: it does not accept multi-line blocks nor the return instruction because the evaluated value of the expression IS automatically its result.
In Python, functions are first-class objects: you can assign them, put them in lists, and pass them as arguments to higher-order functions like sorted, min, or max. lambda is used precisely to pass a disposable projection micro-rule (e.g. key=lambda u: u["age"]) without burdening the code with a service def.
Sections covered in this lesson
- 1The mental model: function as a first-class value
- 2Ideal use with
sorted,max,minand closure functions - 3Quick check
- 4Test yourself
Unlock "Lambdas and higher-order functions" and the full Python Course curriculum
Anonymous functions with lambda, use with sorted/max/min, when to prefer def, and sorted(..., key=...).
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Python Course plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.