Python 3x Pandas Django

Python Lambda


A lambda function is a small anonymous function

These functions are throw-away functions, i.e. they are just needed where they have been created

A lambda functions are mainly used in combination with the functions filter(), map() and reduce()

The lambda feature was added to Python due to the demand from Lisp programmers

Syntax of Lambda Function

lambda arguments: expression

The argument list consists of a comma separated list of arguments and the expression is an arithmetic expression using these arguments. You can assign the function to a variable to give it a name.

Example of a lambda function returns the sum of its two arguments

sum = lambda x, y : x + y
sum(10,22)    #Output: 32

The following example of the conventional function returns the sum of its two arguments

def sum(x,y):
    return x + y

sum(10,22)    #Output: 32

As we have mentioned earlier, the advantage of the lambda operator can be seen when it is used in combination with the map() function. It is called Lambda Expressions. map() is a function which takes two arguments:

Lambda Expressions

r = map(func, seq)

a. The first argument func is the name of a function and the second a sequence (e.g. a list) seq.

b. map() applies the function func to all the elements of the sequence seq.

c. map() returns an iterator

Working of map()
def calculateSquare(n):
    return n*n

numbers = (1, 2, 3, 4)
result = list(map(calculateSquare, numbers))
result = list(result)
print(result)       #Output: [1, 4, 9, 16]

In the example above we haven't used lambda. By using lambda, we wouldn't have had to define and name the functions calculateSquare(n). You can see this in the following interactive session

numbers = (1, 2, 3, 4)
result = map(lambda x: x*x, numbers)
result = list(result)
print(result)       #Output: [1, 4, 9, 16]

map() can be applied to more than one list. The lists don't have to have the same length. map() will apply its lambda function to the elements of the argument lists, i.e. it first applies to the elements with the 0th index, then to the elements with the 1st index until the n-th index is reached:

a = [1, 2, 3, 4]
b = [17, 12, 11, 10]
c = [-1, -4, 5, 9]

print(list(map(lambda x, y, z : (x+y+z), a, b, c)))   #Output: [17, 10, 19, 23]

print(list(map(lambda x, y : (x+y), a, b)))           #Output: [18, 14, 14, 14]

print(list(map(lambda x, y, z : (2.5*x + 2*y - z), a, b, c)))

#Output: [37.5, 33.0, 24.5, 21.0]

If one list has fewer elements than the others, map will stop when the shortest list has been consumed:

a = [1, 2, 3]
b = [17, 12, 11, 10]
c = [-1, -4, 5, 9]

print(list(map(lambda x, y, z : (2.5*x + 2*y - z), a, b, c)))

#Output: [37.5, 33.0, 24.5]

You can see in the example above that the parameter x gets its values from the list a, while y gets its values from b, and z from list c.

If you have any doubts or queries related to this chapter, get them clarified from our Python Team experts on ibmmainframer Community!