NewWork mode: your Claude Code or Codex on work calls, answering from your project's codeSkip to content
IT glossary
ProgrammingUpdated December 3, 2026

Lambda function

Лямбда-функция

A lambda function is a function you can define without giving it a name. It lets you write a small operation right where it’s needed, such as telling a sort operation which field to use.

Also known as: лямбда, анонимная функция

What is a lambda function?

A lambda expression creates a function as part of another expression. For example, instead of defining a function called get_price before a sort, you can write the price lookup directly in the call. The sort operation receives that lookup as a function it can call.

The function is often called anonymous because its syntax doesn’t require a name. You can still assign it to a variable, pass it as an argument, or return it from another function. Anonymous function and lambda aren’t always interchangeable terms: JavaScript’s x => x * 2 is an arrow function, but function (x) { return x * 2; } is also anonymous when used as an expression.

How it works in Python

In lambda item: item["price"], item is the parameter, and the expression after the colon supplies the return value. Evaluating the lambda creates the function; the lookup happens only when that function is called. Python limits a lambda’s body to one expression. That’s a Python rule, not a universal limitation: JavaScript arrow functions can also have block bodies with multiple statements.

Example: give a sort operation a key

items = [
    {"name": "notebook", "price": 8},
    {"name": "pencil", "price": 2},
]
by_price = sorted(items, key=lambda item: item["price"])

The key argument receives the function itself. sorted calls it once per item and sorts by the resulting keys, 8 and 2. The new list puts the pencil first and leaves items unchanged. The lambda doesn’t sort anything on its own; it just tells sorted what to compare.

When to use one

Lambdas work well for short sort keys, filter conditions, transformations, and callbacks. If readers have to untangle several branches or infer an important business rule from the expression, use a named function. A name such as is_eligible_for_refund can explain why a callback exists without forcing readers to work through its implementation first. Don’t assume a lambda is pure: it can call something that mutates a list or otherwise changes state.

What comes up in interviews

In Python interviews, watch for lambdas created in a loop. If funcs = [lambda: n * 10 for n in range(3)], calling each one afterward returns [20, 20, 20], not [0, 10, 20]. The functions share a binding for n and look up its value when called, after the loop has left it at 2. Writing lambda n=n: n * 10 instead evaluates the default argument when each function is created, so each no-argument call uses the intended number.