lambda

A small function written inline as an expression, such as lambda x: x * 2, with no name and a single expression as its body.

In more detail

Lambdas are handy where a short function is needed once, such as a sort key. Anything longer or reused deserves a named function defined with def.

Examples

lambda v: json.dumps(v).encode()
sorted(pairs, key=lambda p: p[1])

Related terms

Taught in

References

Certification courses