Lambda in Python

Besides the def statement, Python also provides a form of expression that generates function objects. Due to its similarity to a Lisp language tool, it is called lambda. In this article, I will introduce you to the Lambda function in Python.

What is Lambda in Python?

Like def, the Lambdas expression creates a function to be called later but returns the function instead of assigning it to a name. Because of this lambdas are like anonymous functions in python. In practice, they are often used to insert a function definition or to delay the execution of a piece of code.

Also, Read – SARIMA Model in Machine Learning.

The general form of lambdas is the keyword lambda, followed by one or more arguments (just like the list of arguments you enclose in parentheses in a def header), followed by an expression after a colon:

lambda argument1, argument2,... argumentN : expression using arguments

Function objects returned by executing lambda expressions work exactly the same as those created and affected by defs, but there are a few differences that make lambdas useful in specialized roles.

  • lambda is an expression, not a declaration
  • The body of lambda is a single expression, not a block of statements

Apart from these distinctions, defs and lambdas do the same kind of work. For example, here’s how to create a function with a def statement:

def func(x, y, z): return x + y + z print(func(2,3,4))
Output: 9

But you can achieve the same effect with a lambda expression by explicitly assigning its result to a name by which you can call the function later:

f = lambda x, y, z: x + y + z print(f(2,3,4))
Output: 9

Here, we attribute to f the function object created by the expression lambda; this is how def works too, but its assignment is automatic.

Default values ​​work on lambdas arguments, just like in a def:

x = (lambda a = "fee", b = "fie", c = "foe": a + b + c) print(x("wee"))
Output: weefiefoe

Code in lambdas body also follows the same scoping rules as code inside a def. Lambda expressions introduce local scope much like a nested def, which automatically sees names in bounding functions, module, and built-in scope:

def knights(): title = "Sir" action = (lambda x:title + " "+ x ) return action act = knights() msg = act("robin") print(msg) print(act)
Output:  Sir robin
. at 0x00000000029CA488>

Why Use lambda?

Generally speaking, the lambdas come in handy as a kind of function shortcut that allows you to embed the definition of a function in the code that uses it.

They are completely optional, you can always use def instead, and should if your function requires the power of full instructions that the lambda expression cannot easily provide, but they tend to be simpler coding constructs in scenarios where you just need to embed small pieces of inline executable code where it needs to be used.

Also, Read – How to Launch a Machine Learning Model?

I hope you liked this article on the Lambda expressions in python. Feel free to ask your valuable questions in the comments section below. You can also follow me on Medium to learn every topic of Machine Learning and Python.

Get Daily Newsletters

Leave a Reply