Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Const

lambda

A lambda expression creates an anonymous function object from an optional parameter list and one expression. Calling it returns the value produced by that expression.

How it works

A lambda expression creates a function object without assigning it a name. Its parameter list is optional, and its body is exactly one expression whose evaluated value becomes the function call's result; it cannot contain statements or annotations.

Syntax

lambda [parameter_list]: expression

Parameters

NameTypeDescription
parameter_listparametersAn optional ordinary function parameter list, using the syntax described for function definitions.

Returns

A function object whose call result is the value of the body expression.

Key points

  • A lambda creates an anonymous function for use where a function object is needed.
  • Evaluating the expression produces a callable function object rather than immediately producing the body value.
  • Its behavior is equivalent to defining a function with def whose body returns the lambda expression's value.
  • Lambda parameters follow the same parameter-list syntax documented for ordinary function definitions.
  • The body is deliberately limited: statements and annotations are not allowed.

Common mistakes

  • A lambda body cannot include statements or annotations; use a regular function definition when that structure is needed.
  • A lambda is still a function object, but it has no explicit source-level function name.

Example: Apply a small function with map

numbers = [3, 1, 2]
squares = list(map(lambda n: n * n, numbers))
print(squares)

The lambda accepts one number and returns its square; map applies it to every item.

When to use it

Inline callback

Pass a short transformation or key function directly to an API that accepts a callable.

Small one-expression function

Create a compact callable for a simple calculation without a separate named definition.

Interview questions

QWhat does a lambda expression produce?

The syntax is lambda followed by an optional parameter list, a colon, and one expression. Evaluating it produces a function object, and calling that function returns the expression's value.

Follow-up: Can a lambda contain an assignment statement or a return statement? No: its body is restricted to an expression, so use def for statement-based logic.

QWhat is the syntax of a lambda expression?

A lambda can omit parameters, accept parameters, and use the normal parameter-list rules. For example, lambda a, b: a + b creates a two-argument function.

Follow-up: How would you support defaults or keyword-only parameters? Use the corresponding forms permitted in a normal function parameter list.

QWhen would you use a lambda instead of def?

Lambda expressions are useful when a small function object is needed inline, such as passing a transformation to map. The resulting object behaves like a function defined with def, except the lambda syntax is limited to one expression and does not provide a named definition.

Follow-up: When should a regular function be preferred? Prefer def when the logic needs statements, annotations, or a clearer reusable definition.

  • function definitions
  • parameter lists
  • function object
  • expression

Source: 6. Expressions — Python 3.14.7 documentation. Checked 2026-10-16.