In Python we see constructs that might be unfamiliar to us. Python tries to make things easy to use and gives very powerful constructs in the form of special functions. One of those is Lambdas. These are anonymous functions, kind of use, and throw function. We don’t want to call them over and over again. Define, use them, and don’t bother to utilize them in the future.
A Lambda function i.e. a function with no name is always preceded by a name ‘lambda’ and after the keyword, we specify the input arguments for the function. For eg.
This lambda function takes 2 input arguments x and y. These functions don’t use the ‘def’ keyword as they are named function. We specify the 2 input parameters after the lambda keyword and then the body of the function starts. So, a lambda function takes arguments as x and y and a function on x to y will be returned and unlike any other function a return statement doesn’t need to be specified. In a more generic way, whatever calculation we perform in lambda, the result of that will be returned and could be assigned to a variable as illustrated in the image below.
We will take a working example and things would get more clearer.
addition_func = lambda x, y: x + y
Now, we will pass these 2 numbers which need to be added,
OUTPUT 50addition_func(145, 536)
They are typically used with lists if one has to apply the lambda function to all elements of a list.
Number_list = [31, 15, 13, 93, 7, 54, 92]
Now, if we want to filter all numbers which are greater than 50, we can use a lambda function to do this.
greater_than_30 = lambda x: x > 30
This will return TRUE if greater than 30, FALSE otherwise, and assign all these to greater_than_30 variable. In order to apply the above function to every element of the list, we use filter function. FIlter function contains the condition on which we want to filter the list and the list itself.
result = filter(greater_than_30, Number_list)
to print the output of the result, we would have to convert it into a list.
result = list(result)
[93, 54, 92]
As lists are of various types, this time we would take a list of strings. The objective is to find out the strings which are palindrome out of all names.
text = ["php", "w3r", "Python", "abcd", "Java", "aaa"]result = list(filter(lambda x: (x == "".join(reversed(x))), text))
Here, we have also used join function which is a string method and returns a string in which the elements of the sequence have been joined by an str separator.
It generates a new list by applying a function to each element of the list.
Num_list =[1,2,3,4,5]squares = lambda x: x *2
result = list(map(squares, Num_list))
output - [1,4,9,16,25]
In another example, we will reverse each string in the list of strings.
Names_list= ['Sachin', 'Saurav' , 'Kohli', 'pip', 'Rohit', 'Bumrah']
ReversedList = list(map(lambda x : x[::-1], Names_list))
['nihcaS', 'varuaS', 'ilhoK', 'pip', 'tihoR', 'harmuB']
It again has got 2 input variables, x stores the aggregated function of all the values of the list so far, and y stores the current value of the iterator or the new element that we encounter in the list.
Eg. Let's do a sum of all elements of a list of numbers
Num_list = [13, 15, 213, 193, 524, 192]sum_func = lambda x, y: x + yfrom functools import reduceresult = reduce(sum_func, Num_list)
In the next posts, we will try to cover other special functions like lists comprehension which would make the code cleaner and faster. Also, there will be a separate post on classes and inheritance concepts in python which would help you to code more efficiently and with lesser maintenance.
So, follow me for such useful posts in the future.
To read about my earlier posts, click below-