The Lambda expression is fully parsed in Python

  • 2020-05-17 05:48:47
  • OfStack

What is an Lambda expression

The "Lambda expression" (lambda expression) is an anonymous function, and the Lambda expression, named after the calculus in mathematics, corresponds directly to the lambda abstraction (lambda abstraction) and is an anonymous function, that is, a function without a function name. The Lambda expression can represent closures (note the difference from the traditional mathematical sense).

Lambda is an anonymous function that can be replaced by an lambda expression when we need to call a function repeatedly and don't want to write so much code.

General format of lambda:

lambda argument: manipulate(argument)

Sample code:

add = lambda x,y : x + y
print add(3,5)
#output: 8

Usage:

Sorting.


a = [(1, 2), (4, 1), (9, 10), (13, -3)]
a.sort(key=lambda x: x[1])
print(a)
# Output: [(13, -3), (4, 1), (1, 2), (9, 10)]

Related articles: