Menu

Python Lambda: Anonymous Functions, sort key, and map/filter

What lambda is, what it's good for, and the handful of places where it actually earns its keep — plus when to reach for a named function instead.

A Function You Don't Bother Naming

Most functions in Python are defined with def, have names, and live in a file where they can be reused. A lambda is a smaller, unnamed alternative — useful in the handful of cases where defining a full function would be more ceremony than it's worth.

Here's the shape side by side:

main.py
Output
Click Run to see the output here.

Two functions, both doing the exact same thing. The lambda version fits on one line because that's basically its job: a throwaway function for a throwaway moment.

Syntax

lambda <params>: <expression>

  • <params> is the same parameter list a def would use — supports defaults, *args, **kwargs.
  • <expression> is a single expression whose value becomes the return value.

Examples:

main.py
Output
Click Run to see the output here.

No return keyword. The expression's value is returned implicitly.

The One Limitation

A lambda can contain only a single expression. You can't put statements in it:

main.py
Output
Click Run to see the output here.

But you can use the ternary expression:

main.py
Output
Click Run to see the output here.

If you need more than one expression, that's your cue to switch to def.

Where Lambdas Actually Earn Their Keep

A lambda assigned to a variable is almost always worse than a named def:

main.py
Output
Click Run to see the output here.

That's strictly worse than def double(x): return x * 2 — same readability, plus the lambda version gives debuggers the unhelpful name <lambda> in stack traces.

Lambdas shine in exactly one spot: as inline arguments to higher-order functions.

Custom sort keys

The classic case:

main.py
Output
Click Run to see the output here.

The key argument of sorted wants a function that takes an item and returns the value to sort by. A one-line lambda is exactly right — and using a full def would feel heavy for something this small.

You can sort by multiple fields using a tuple:

main.py
Output
Click Run to see the output here.

min, max, filter, map

Similar story — they all accept an optional function argument:

main.py
Output
Click Run to see the output here.

For map and filter, a list comprehension is usually even clearer:

main.py
Output
Click Run to see the output here.

Most Pythonistas reach for comprehensions over map/filter for this reason. lambda remains a natural fit for sorted, min, and max where there's no comprehension shortcut.

Lambdas and operator

When your lambda is just "grab a key" or "grab an attribute," Python's operator module has faster, clearer alternatives:

main.py
Output
Click Run to see the output here.

Not a huge win, but in code that already imports operator it reads a bit more declaratively.

When to Switch Back to def

If any of these are true, write a def:

  1. The logic doesn't fit cleanly in a single expression.
  2. You want to name the function because the name would aid readability.
  3. You'd use the same logic in more than one place.
  4. The function needs a docstring (lambdas can't carry one).

Remember: Python isn't scored on line count. A three-line named function almost always reads better than a one-line lambda once the logic is non-trivial.

Next: Decorators

Lambdas hand functions around as values. Decorators take the same idea one step further: they let you wrap any function with reusable behavior — logging, timing, caching, auth — using a single @ on the line above. That's the next page.

Frequently Asked Questions

What is a lambda in Python?

A lambda is a short, anonymous function you can write on a single line. lambda x: x * 2 is equivalent to def double(x): return x * 2, except without a name. It's most useful when you need a tiny function as an argument to another function.

When should I use lambda instead of def?

Use lambda for one-off, one-expression functions that you pass directly to something like sorted, max, or filter. For anything with a body longer than a single expression — or for anything you'd name — use def. Named functions are almost always clearer.

What are the limits of lambda?

A lambda can contain only a single expression, not statements. No assignments, no if blocks (though you can use the ternary a if cond else b), no multi-line logic. If you're fighting those limits, switch to def.

Learn to code with Coddy

GET STARTED