How to use the Python Lambda Function
A Lambda function in Python is an anonymous function meaning that it does not have a name. This is useful if you need to create a small function and want the cleanest syntax possible.
To demonstrate how Python Lambda functions work, let's first look at an example of how we would write a regular named function and turn it into an anonymous function.
def addValues(a, b):
return a + b
result = addValues(9, 17)
print(result)
26
The example function above takes two arguments, adds them together and returns the result.
Because it is such a simple operation, we might consider turning it into a lambda function and assigning it to a variable.
a = lambda a, b: a + b
result = a(9, 17)
print(result)
26
Call Lambda Function Without Assigning it to a Variable
A lambda function does not need to be assigned to a variable. Put the function inside parenthesis and pass the arguments after it like this:
print((lambda a, b: a + b)(9, 17))
26