A function without a name is called 'anonymous function'. So far, the functions we wrote were defined using the keyword 'def'.But anonymous functions are not defined using 'def'. They are defined using the keyword lambda and hence they are also called 'Lambda functions'. Let's take a normal function that returns square of a given value.
The same function can be written as anonymous function as:
lambda x: x*x
The format of lambda functions is:
lambda argument_list: expression
Normally, if a function returns some value, we assign that value to a variable as:
y = square(5)
But, lambda functions return a function and hence they should be assigned to a function as:
f = lambda x: x*x
Here, 'f' is the function name to which the lambda expression is assigned. Now, if we call the function f() as:
value = f(5)
Now, 'value' contains the square value of 5, i.e. 25
Lambda functions contain only one expression and they return the result implicitly. Hence we should not write any 'return' statement in the lambda functions.
Because a lambda functions is always represented by a function, we can pass a lambda function to another function. It means we are passing a function (i.e. lambda) to another function. This makes processing the data very easy. For example, lambda functions are generally used with functions like filter(), map() or reduce().
The filter() function is useful to filter out the elements of a sequence depending on the result of a function. We should supply a function and a sequence to the filter() function as:
Here, the 'function' represents a function name that may return either True or False; and 'sequence' represents a list, string or tuple. The 'function' is applied to every element of the 'sequence' and when the function returns True, the element is extracted otherwise it is ignored. Before using the filter() function, let's first write a function that tests whether a given number is even or odd.
Now, we can use this function inside filter() to test the elements of a list 'lst' as: filter(is_even, lst)
Passing lambda function to filter() function is more elegant.
Function The map() function is similar to filter() function but it acts on each element of the sequence and perhaps changes the elements. The format of map() function is:
The 'function' performs a specified operation on all the elements of the sequence and the modified elements are returned which can be stored in another sequence.
It is possible to use map() function on more than one list if the lists are of same length. In this case, map() function takes the lists as arguments of the lambda function and does the operation. For example,
map(lambda x, y: x*y, lst1, lst2)
Here, lambda function has two arguments 'x' and 'y'. Hence, 'x' represents 'lst1' and 'y' represents 'lst2'. Since lambda is showing x*y, the respective elements from lst1 and lst2 are multiplied and the product is returned.
The reduce() function reduces a sequence of elements to a single value by processing the elements according to a function supplied. The reduce() function is used in the format:
For example, we write the reduce() function with a lambda expression, as:lst = [1, 2, 3, 4, 5]
reduce(lambda x, y: x*y, lst)
The lambda function is taking two arguments and returning their product. Hence, starting from the 0th element of the list 'lst', the first two elements are multiplied and the product is obtained. Then this product is multiplied with the third element and the product is obtained. Again this product is multiplied with the fourth element and so on. The final product value is returned, as shown in Figure