Lambda Function

Huda
4 min readApr 2, 2021

--

from https://realpython.com/python-lambda/

Lambda function is a function that is defined without a name, this is why it is called an anonymous function. Usually, when defining a function in python; we use a def keyword to define followed by the name of the function, then the parameter list followed by a colon and the function body in the second line.

Nevertheless, the Lambda function doesn’t follow the general syntax of a function in python, Lambda function is defined by using the keyword lambda followed by an argument that could be one or more argument(s), then only one expression and it can be stored in a variable.

Lambda functions can come in handy when we need a function for a short period, or it can be used as the argument inside another function like a higher-order function which is a function using one or more arguments as a function or a return function as its outcome.

Lambda function is also used with python built-in functions like map, filter, and reduce.

Map Function

From https://realpython.com/python-map-function/

The map function is a Python built-in function that is used to apply a function to all elements of a sequence (list, tuple, strings, range, dictionary).
The syntax of the map function is map(function, sequence). Lambda function comes in handy inside the map function without the need of defining a function outside of the map function.

We should wrap the map function with a list or tuple to get the outcome as a list or tuple.

Filter Function

from https://realpython.com/python-functional-programming/

The filter function is a function that will filter out the element of a sequence that meets a certain condition, but unlike the map function, the filter function can only take one sequence. The filter function follows the same syntax as the map function: filter(function, sequence). Not to mention the Lambda function can be useful inside the filter function.
Example,In a range of numbers from 1 to 11, I want to print the numbers that are less than 7, so I used a for loop for that.

The same thing can be done by using the filter function with lambda and it can be done with one line code. We should wrap the filter function with a list to get the outcome as a list.

Reduce Function

from https://realpython.com/python-reduce-function/

This function will reduce the sequence of elements to a single element by using a function, it is usually used when we want to find out the sum of all elements in the list, or the product of those elements in the list. It also follows the same syntax as filter and map function: reduce(function, sequence)
To use reduce function, we have to import it first from the functools module, so in this example, the lambda function will add all the numbers of the list to get one element.

I hope you find this article is helpful.

--

--

Huda

Data Scientist with recent experience in data acquisition and data modeling, statistical analysis, machine learning, deep learning and NLP