The syntax for a lambda function is
lambda arguments: expression
Here is an example: This function squares its argument
g = lambda x: x**2
print("g(5) = ", g(5))
('g(5) = ', 25)
def f(x): return x**2 print("f(5) = ", f(5))
('f(5) = ', 25)
Lambda functions can come in handy if you do not want to define a separate function. Assume you have a list and you want to find all list elements which are even
a = [2, 3, 6, 7, 9, 10, 122] print(filter(lambda x: x % 2 == 0, a))
[2, 6, 10, 122]
Note that many programmers don't like lambda functions, but rather use list comprehension, which they deem more readable. The example above written with a list comprehension would look like this
print([num for num in a if num % 2 == 0])
Maybe just to extend on this we should discuss two examples where list comprehension does not work. Let's assume we want to sort a list by the modulus of 10 of the value. We can do that with
print(sorted(a, key=lambda x: x % 10))
[10, 2, 122, 3, 6, 7, 9]
def gaussian(height, center_x, center_y, width_x, width_y): return lambda x,y: height*np.exp( -(((center_x-x)/width_x)**2+((center_y-y)/width_y)**2)/2)
func = gaussian(1., 0.5, 0.5, 1., 1.) print(func(0.2, 0.8))
And there are many more examples where lambda functions can come in very handy. In any case, it is important to be familiar with the concept of lambda functions, even just to be able to read other people's code.
Besides the filter function, there are also map() and reduce() which often make use of lambda functions. The map() function maps all values of a list to another list using a function like this
a = [2, 3, 6, 7, 9, 10, 122] b = [121, 32, 61, 45, 78, 1, 90] print(map(lambda x, y: x+y, a, b))
[123, 35, 67, 52, 87, 11, 212]
The reduce function is a bit different since it is executed multiple times. The function which needs to be fed into reduce() has to accept two arguments. This function is then called on the first two elements of the list, then with the result of that call and the third element, and so on, until all of the list elements have been processed
from functools import reduce a = [2, 3, 6, 7, 9, 10, 122] print(reduce(lambda x,y: x+y, a))
159
Lambda functions can be used anywhere, but in my case, they rarely appear outside of filter(), map() and reduce(). I hope that was useful, please leave questions/comments below.
cheers
Florian
I appreciate you taking the time and effort to share your knowledge. This material proved to be really efficient and beneficial to me. Thank you very much for providing this information. Continue to write your blog.
ReplyDeleteData Engineering Services
Machine Learning Services
Data Analytics Services
Data Modernization Services
I appreciate you taking the time and effort to share your knowledge. This material proved to be really efficient and beneficial to me. Thank you very much for providing this information. Continue to write your blog.
ReplyDeleteData Engineering Services
Machine Learning Services
Data Analytics Services
Data Modernization Services