Understanding Lambda Functions in Python

·

3 min read

Lambda functions in python are anonymous functions. Anonymous functions refers to kind of functions that have no name. Although they look syntactically different, lambda functions behave in the same way as regular functions.

python-lambda-functions-new.png

Why even use Lambda functions?

It's easier to write the function that doesn't take up more lines of code. These are also used to create functions in a short amount of time.

Syntax:

lambda arguments : expression

A lambda function can take up any number of arguments, but restricted to have only one expression.

Let us look at an example: Suppose we write a function to multiply given number with 3. Using regular functions, we write it as:

def mult5(num):
    return num*5

Using lambda functions, we write it as:

mult5 = lambda num:num*5
print(mult5(2)) # prints 10

As we can see, it works just like any ordinary function. These functions can minimize the burden of creating new regular functions that only return single expression. This is also extensively used while working with map and filter functions.

Lambda functions can also be used inside of other functions. Let us see an example:

def func(num):
    mult = lambda x,y: x*y

    return mult(5,4)+10 + num

print(func(18))  #prints 48

Here, we can implemented a lambda function inside a regular function that contains multiple parameters. This has reduced a lot of code without unnecessarily having to create mult function outside func. A parameter that is given to main function can also be rendered to lambda function as an argument. This way, it keeps it all contained and compact.

Now in this section, let us look at the use cases of lambda function with map() and filter() functions.

With Map() functions: The map() function is another built-in function that takes a function object and a list. It;s basically used to create a list quickly. The syntax of map function is as follows:

map(object, iterable_1, iterable_2, ...):
lst = [1,2,3,4,5]
newLst = list(map(lambda num:num+10,lst))
print(newLst)  # prints [11,22,33,44,55]

Using lambda function with map(), we can add value(here 10) to each element of the list. This saves us time by not having to create another function that we're using here for a specific case.

With Filter() function: The Python's filter() function takes a lambda function together with a list as the arguments. It has the following syntax:

filter(object, iterable)

The object here should be a lambda function which returns a boolean value. The object will be called for every item in the iterable to do the evaluation. The result is either a True or a False for every item. Lambda functions works well with filter functions too. We can take up the last example and check it with using filter() functionality:

lst = [1,2,3,4,5]
newLst = list(filter(lambda num:num>=3,lst))
print(newLst)  # prints [3, 4, 5]

Here, filter() checks for numbers which are greater than or equal to 3 using lambda function by taking input as lst and finally prints it out as newList.

As we see from the above examples, lambda functions get really useful when creating functions within a function and keeping codebase organized. At often times most lambda function is passed as arguments to another function.