Language Syntax

Lambda — Python syntax

Create a function whose body is a single expression and whose result is returned implicitly. Statements and parameter annotations are not allowed; defaults and closures work as with def.

Formal Syntax & Structure

python syntaxpython
lambda parameters: expression

Parameters & Attributes

NameTypeDescription
parametersparameter listOptional function parameters.
expressionexpressionSingle expression returned by the function.

Return Value

function

Value obtained by evaluating this expression; see description for context and constraints.

Working Example

Example Snippetpython
items=['bbb','a','cc']print(sorted(items,key=lambda s: len(s)))
Python Reference