Learn lambda functions through real examples

Photo by Tolga Ulkan on Unsplash

One advanced Python concept is lambda functions, which are anonymous functions that are defined using the lambda keyword. Lambda functions have the following basic form:

lambda params: expression
  • The keyword lambda signifies that you are defining a lambda function.
  • params refers to the parameters that the lambda function will use. The number of the parameters can be zero to multiple.
  • expression refers to the expression that the lambda function will operate.

At their core, lambda functions are just like other Python functions, which can be invoked using the call operator (the parentheses). The following code shows you related facts.

>>> multiply = lambda x, y: x*y
>>> type(multiply)
<class 'function'>
>>> multiply(5, 3)
>>> multiply(8, 5)

Although we assigned the lambda function to the variable multiply, it’s just for showing you the fact that lambdas are functions. In real-life projects, this practice is strongly discouraged, because as known by its alternative name, lambdas are anonymous functions, which should be used in a place where the explicit definition of a function is not needed.

After knowing how to define a lambda function, let’s explore its actual use cases to learn this technique more thoroughly.

Continue reading: https://towardsdatascience.com/python-lambda-functions-three-practical-examples-sort-map-and-apply-286593792cb4?source=rss—-7f60cf5620c9—4

Source: towardsdatascience.com