Lambda Functions in Python Code

madan
3 min readDec 27, 2019

Lambdas in Python

In Python, lambda expressions (or lambda forms) are utilized to construct anonymous functions. To do so, you will use the lambda keyword (just as you use def to define normal functions).

Every anonymous function you define in Python will have 3 essential parts:

The lambda keyword. The parameters (or bound variables), and The function body. A lambda function can have any number of parameters, but the function body can only contain one expression.

Moreover, a lambda is written in a single line of code and can also be invoked immediately. You will see all this in action in the upcoming examples.

In [36]:

(lambda x : x + x )(2)

Out[36]:

4

In [45]:

a=lambda x : x + x
print(a(2))
4

In [1]:

c = lambda x:2*x
print(c(2))
4

In [2]:

(lambda x:2*x)(6)

Out[2]:

12

In [3]:

def double(x):
return x*2
double(2)

Out[3]:

4

In [4]:

def double(x):
print(x*2)
double(2)
4

In [5]:

def add(x,y):
return x+y
add(2,5)

Out[5]:

7

In [6]:

a = lambda x,y:x+y
a(4,8)

Out[6]:

12

In [7]:

x = 'some kind of useless lambda'
(lambda x: print(x))(x)
some kind of useless lambda

In [8]:

(lambda x:x + x)(4)

Out[8]:

8

In [11]:

def mx(x,y):
if x > y:
return x
else:
return y
print(mx(8,5))
o/p:8

In [18]:

mx = lambda x,y:x if x>y else y
print(mx(8,5))
o/p:8

lambdas in map()

the map function is used to apply a particular operation to every element in a sequence. Like filter(), it also takes 2 parameters:

A function that defines the top to perform on the elements One or more sequences

In [19]:

sequences = [10,2,8,7,5,4,3,11,0, 1]
filtered_result = map (lambda x: x*x, sequences)
print(list(filtered_result))
o/p:[100, 4, 64, 49, 25, 16, 9, 121, 0, 1]

In [20]:

sequences = [10,2,8,7,5,4,3,11,0, 1]
for i in sequences:
print(i*i)
o/p:100
4
64
49
25
16
9
121
0
1

In [24]:

#print[16,9,4,1]
def square(lst1):
lst2 = []
for num in lst1:
lst2.append(num**2)
return lst2
print( square([4,3,2,1]))
o/p:[16, 9, 4, 1]

In [31]:

n = [4,3,2,1]
print(list(map(lambda x:x**2 ,n)))
o/p:[16, 9, 4, 1]

In [48]:

my_strings = ['a', 'b', 'c', 'd', 'e']
my_numbers = [1,2,3,4,5]
results = list(map(lambda x, y: (x, y), my_strings, my_numbers))print(results)o/p:[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]

In [49]:

def newfunc(a):
return a*a
x = map(newfunc, (1,2,3,4)) #x is the map object
print(x)
print(list(x))
o/p:<map object at 0x7f502c8c4fd0>
[1, 4, 9, 16]

In [27]:

print([x**2 for x in n])#list comprehensiono/p:[16, 9, 4, 1]

filter

  • filter items out of sequences
  • return filtered list

Using lambda within filter(): The lambda function that is used as a parameter actually defines the condition that is to be checked. For example:

In [50]:

y = filter(lambda x: (x>=3), (1,2,3,4))
print(list(y))
o/p:[3, 4]

In [62]:

c =map(lambda x:x+x, (1,2,3,4)) #lambda x: (x>=3)
print(list(c))
o/p:[2, 4, 6, 8]

In [57]:

#prints[4,3]
def over_two(lst1):
lst2=[x for x in lst1 if x>2]
return lst2
print(over_two([4,3,2,1]))
o/p:[4, 3]

In [58]:

n = [4,3,2,1]
print(list(filter(lambda x:x>2, n)))
o/p:[4, 3]

The reduce() function:

The reduce() function, as the name describes, applies a given function to the iterables and returns a single value.

  • applies same operation to items of a sequence
  • uses result of operation as first param of next operation
  • return an item, not a list

In [51]:

from functools import reduce
reduce(lambda a,b: a+b,[23,21,45,98])

Out[51]:

187

In [59]:

n = [4,3,2,1]
print(reduce(lambda x,y:x*y, n))
o/p:24

In [ ]:

def mult(lst)

sing filter() within map(): The code given below first checks for the condition (x>=3) to be true for the iterables. Then, the output is mapped using the map() function.

EXAMPLE:

In [52]:

c = map(lambda x:x+x,filter(lambda x: (x>=3), (1,2,3,4)))
print(list(c))
o/p:[6, 8]

Using map() and filter() within reduce(): The output of the internal functions is reduced according to the condition supplied to the reduce() function.

In [54]:

d = reduce(lambda x,y: x+y,map(lambda x:x+x,filter(lambda x: (x>=3), (1,2,) 
print(d)
o/p:14

Git hub link:

--

--