You heard about Lambda function in python, but you don’t know what it is, and when to use them ? Here is a short explanation.

What is a Lambda function

“The lambda keyword in Python provides a shortcut for declaring small anonymous functions. Lambda functions behave just like regular functions declared with the def keyword. They can be used whenever function objects are required.”
Dan Bader

To sum up the difference for declaring the same function:

When to use Lambda functions

Good practise is to use lambda function only for:

So a good example of a use of a Lambda function is inside map definitions:

  numbers = [1, 2, 3]
  squared_numbers = list(map(lambda x: x**2, numbers))

Tips

You can define your Lambda function before, and it can takes parameters:

  def lambdaFunction(a, b):
      return a + b

  otherParameter = 3

  squared_numbers = list(map(lambda x: lambdaFunction(x,otherParameter), numbers))