🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

lambda Functions

AI & DATA SCIENCE // lambda-functions

A lambda is a small, anonymous, single-expression function, useful for short throwaway operations passed directly as arguments.

Syntax

lambda params: expression

add = lambda a, b: a + b

Deep Dive Course

lambda creates a function object without giving it a name via def, restricted to a single expression whose result is automatically returned — there's no block body, no statements, and no explicit return keyword. Lambdas are most commonly passed directly as the key or condition argument to functions like sorted(), max(), map(), and filter(), where defining a full named function would be unnecessary ceremony for a one-line operation.

1Understanding lambda Functions

lambda creates a function object without giving it a name via def, restricted to a single expression whose result is automatically returned — there's no block body, no statements, and no explicit return keyword. Lambdas are most commonly passed directly as the key or condition argument to functions like sorted(), max(), map(), and filter(), where defining a full named function would be unnecessary ceremony for a one-line operation.

💡

If a lambda is complex enough that it's hard to read on one line, or if it's used in more than one place, define it as a proper named function with def instead — that's exactly the situation lambda isn't meant for.

editor.html
square = lambda x: x ** 2
print(square(6))
localhost:3000

2Practical Example

Here is a real-world application of lambda Functions showing how it is used in production Python code.

editor.html
people = [("Alice", 30), ("Bob", 25), ("Carol", 35)]
by_age = sorted(people, key=lambda person: person[1])
print(by_age)
localhost:3000

3Best Practices

Follow these guidelines when working with lambda Functions:

1. Use lambda for short, one-off functions passed inline to sorted(), map(), filter(), or similar, not for logic you'll reuse elsewhere

2. Assign a def function a real name instead of binding a lambda to a variable name — PEP 8 explicitly recommends def for anything you're going to bind to a name

3. Keep lambda bodies to a single simple expression; reach for def as soon as you need multiple statements or a docstring

⚠️

Tip: If a lambda is complex enough that it's hard to read on one line, or if it's used in more than one place, define it as a proper named function with def instead — that's exactly the situation lambda isn't meant for.

editor.html
square = lambda x: x ** 2
print(square(6))
localhost:3000

Examples

Example 01Basic Usage
square = lambda x: x ** 2
print(square(6))
Example 02Advanced Example
people = [("Alice", 30), ("Bob", 25), ("Carol", 35)]
by_age = sorted(people, key=lambda person: person[1])
print(by_age)

Best Practices

  • Use lambda for short, one-off functions passed inline to sorted(), map(), filter(), or similar, not for logic you'll reuse elsewhere
  • Assign a def function a real name instead of binding a lambda to a variable name — PEP 8 explicitly recommends def for anything you're going to bind to a name
  • Keep lambda bodies to a single simple expression; reach for def as soon as you need multiple statements or a docstring

Interview Question

Why can't a lambda contain multiple statements or a full block of code, the way a regular function defined with def can?

Hint: Think about lambda as syntax for a single expression, not a block.

A lambda is specifically an expression in Python's grammar, not a statement — it's designed to be usable inline, wherever an expression is expected, such as directly inside a function call's argument list. Allowing multiple statements would require some kind of block syntax inside an expression position, which Python's grammar deliberately doesn't support; anything beyond a single expression needs a full def function instead.

Exercises

MediumPractice using lambda Functions in a real scenario.
View Solution
square = lambda x: x ** 2
print(square(6))

Frequently Asked Questions

Why can't a lambda contain multiple statements or a full block of code, the way a regular function defined with def can?

A lambda is specifically an expression in Python's grammar, not a statement — it's designed to be usable inline, wherever an expression is expected, such as directly inside a function call's argument list. Allowing multiple statements would require some kind of block syntax inside an expression position, which Python's grammar deliberately doesn't support; anything beyond a single expression needs a full def function instead.

Related Functions

def-keywordsorted()map()