Skip to main content
Module 5 · Lesson 4 of 420/36 in the course~14 min
Module lessons (4/4)

Lambdas and higher-order functions

Lesson preview5 hands-on exercises readyPython Course · Module 5 — 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 lambda and built-in functions as arguments key= to higher-order functions (sorted, max, min).
  • Recognize when def and explicit closures offer superior readability and debugging than lambda.

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

  1. 1The mental model: function as a first-class value
  2. 2Ideal use with sorted, max, min and closure functions
  3. 3Quick check
  4. 4Test yourself
Pro Lesson · Module 5 — Functions

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.