The lambda Operator
To create an anonymous function in the form of an expression, use the lambda statement:
lambda args : expression
args is a comma-separated list of arguments, and expression is an expression involving those arguments. For example:
a = lambda x,y : x+y print a(2,3) # produces 5
The code defined with lambda must be a valid expression. Multiple statements and other non-expression statements, such as print, for, and while, cannot appear in a lambda statement. lambda expressions follow the same scoping rules as functions.
Get Python: Essential Reference, Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.