Lambda Functions

Sometimes you need a function for just a moment — a tiny rule that says "sort by this" or "double that" — and writing a full def feels heavy. A lambda is a one-line, throwaway function built exactly for these passing needs.

Learn Lambda Functions in our free Python course — a beginner-friendly interactive lesson with runnable examples, a practice exercise and a quick recall.

Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

Their natural home is the key argument of sorted() , max() , and min() , plus quick use with map() and filter() .

A lambda is a normal function squeezed onto one line. Compare the two — they behave identically:

There's no return keyword — the expression's value is the return value. Lambdas can take zero, one, or many parameters.

A lambda body must be a single expression — something that produces a value. You cannot put statements (if-blocks, loops, assignments) inside one:

The trick is the conditional expression ( a if cond else b ) — it looks like a mini if/else but evaluates to a single value, so it's perfectly legal in a lambda.

This is where lambdas earn their keep. The key argument of sorted() takes a function that decides what to sort by :

The lambda is applied to each element to produce a sort value, but the original elements are what get returned — just reordered. This single pattern accounts for the vast majority of lambdas you'll write.

The same key idea works with max() and min() to find the "biggest" or "smallest" by any rule you like:

map() applies a function to every item; filter() keeps items where the function returns True. Lambdas supply that function inline:

These lines should find the longest word in a list and print it. Reorder them so the output is Longest: elephant .

Why: the words list (B) must exist before max (A) can scan it. The lambda len(w) tells max to compare by length, so it returns elephant (8 letters). The print (C) comes last.

15 — the lambda takes x and returns x + 10 ; calling it with 5 gives 15.

[(2, 'a'), (1, 'b')] — sorting by p[1] orders by the letters, so "a" comes before "b".

[3, 4] — filter keeps only items where the lambda returns True, i.e. numbers greater than 2.

Given a list of (filename, size_kb) tuples, sort and summarize them with lambdas.

Lesson complete — lambdas demystified!

You can write one-line lambdas, understand the single-expression rule, and use them where they truly shine: the key argument of sorted() , max() , and min() . And you know when a plain def is the better choice.

🚀 Up next: enumerate & zip — loop with indexes and walk multiple lists in parallel.

Practice quiz

What is a lambda?

  • A class definition
  • A type of loop
  • A small anonymous function in a single expression
  • A module

Answer: A small anonymous function in a single expression. A lambda is a tiny anonymous function defined in one expression, e.g. lambda x: x * 2.

Which def is equivalent to: double = lambda x: x * 2?

  • def double(x): return x * 2
  • def double(x): print(x * 2)
  • def double(): return x * 2
  • def double(x): x * 2

Answer: def double(x): return x * 2. A lambda auto-returns its expression, so it matches def double(x): return x * 2.

Does a lambda use the return keyword?

  • Yes, it requires return
  • Only with multiple arguments
  • Only inside sorted()
  • No, the expression's value is returned automatically

Answer: No, the expression's value is returned automatically. There is no return keyword; the single expression's value IS the return value.

What must a lambda body be?

  • A block of statements
  • A single expression
  • A for-loop
  • An assignment

Answer: A single expression. A lambda body must be one expression — no if-blocks, loops, or assignments allowed.

Which is legal inside a lambda?

  • A conditional expression: a if cond else b
  • An if/else block
  • A for-loop
  • x = 5

Answer: A conditional expression: a if cond else b. A conditional expression (a if cond else b) evaluates to a value, so it's allowed.

What does sorted(words, key=lambda w: len(w)) do?

  • Sorts alphabetically
  • Reverses the list
  • Sorts by word length
  • Removes duplicates

Answer: Sorts by word length. The key applies len to each word, so the list is sorted by length.

What does sorted([(1,'b'),(2,'a')], key=lambda p: p[1]) return?

  • b
  • a

Answer: a. Sorting by p[1] orders by the letters, so 'a' comes before 'b': [(2,'a'),(1,'b')].

What does list(filter(lambda n: n > 2, [1,2,3,4])) return?

filter keeps items where the lambda is True, i.e. numbers greater than 2: [3, 4].

What does list(map(lambda n: n * 2, [1, 2, 3])) return?

map applies the lambda to each item, doubling them: [2, 4, 6].

Per PEP 8, what is preferred when you want to NAME a function?

  • Assign a lambda to a variable
  • Use map()
  • Use a def instead of a named lambda
  • Use a class

Answer: Use a def instead of a named lambda. Style guides recommend def over assigning a lambda to a name — it's clearer in tracebacks.