Home » Python

Lambda Expression/function in python

Python Lambda Expression/Function: Here, we are going to learn about the Lambda expression/function in Pythonhow to implement lambda expression/function in python?
Submitted by Yash Khandelwal, on April 13, 2019

Explanation:

In python, there is a function named Lambda. Lambda function is an anonymous function - that means the function which does not have any name.

When we declare a function, we use "def" keyword to define a function with a suitable function name. But lambda function does not require that.

Syntax to declare a lambda expression/function:

    lambda parameterlist : expression

Where,

  • lambda is a reserved word that defines a lambda expression.
  • parameterlist is a comma-separated list of parameters as you would find in the function definition (but notice the lack of parentheses).
  • expression is a single Python expression. The expression cannot be a complete statement.

Note:

  • This function can take as many arguments as it needs but the expression must be single.
  • You are free to use the lambda function wherever you want to use.

Example 1: Elaborating about the difference between the Simple function and the Lambda function.

# simple approach we use to define the area of rectangle:
# Python code to illustrate are of rectangle   
# showing difference between def() and lambda(). 
def area(l,b): 
    return l*b; 
  
g = lambda l,b: l*b 
print('lambda function:',g(7,4)) 
  
#calling the function
print('Via Simple function:',area(7,4)) 

Output

lambda function: 28
Via Simple function: 28

Explanation of the code:

Here, both of the functions return the same area of a rectangle, But while using the def keyword we need to do all the function of the function and also return it. But same in lambda we just need to give the arguments and the expression which returns the answer accordingly. As it does not include any return statement. We can also put a lambda definition anywhere a function is expected, and we don’t have to assign it to a variable at all. This is the simplicity of lambda functions.

Example2: How we can use lambda function different forms?

print('Ways to use and declare lambda functions:')
# simply defining lambda function 
#example - 1
g=lambda x, y: 3*x + y
print('Ex-1:',g(10,2))

#example - 2
f=lambda x, y: print('Ex-2:',x, y)
f(10,2)

#example - 3
h=lambda x, y: 10 if x == y else 2
print('Ex-3:',h(5,5))

#example - 4
i=lambda x, y: 10 if x == y else 2
print('Ex-4:',i(5,3))

Output

Ways to use and declare lambda functions:
Ex-1: 32
Ex-2: 10 2
Ex-3: 10
Ex-4: 2

Lambda functions with filter() , map() , reduce()

lambda() function can be used with the other functions like filter() , map() etc.

filter(): It takes the list of arguments. This function filters out all the elements in the given list which return True for the function.

map(): map() function in python used to map all the elements of the list with its condition by the function or by the lambda function.

Syntax:

    map(function_object, iterable1, iterable2,...)

Example 3: Lambda Function using the filter() , map() , reduce()

#Using the filter() , map() with lambda() function.


# Python code to illustrate 
# filter() with lambda() 
li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61] 
li = list(filter(lambda x: (x%2 == 0) , li)) 
print('By using filter :',li)

# Python code to illustrate  
# map() with lambda()  
# to get double of a list.
l=[{'name':'includehelp', 'star':10},{'name':'yash', 'star':8},{'name':'sanjeev', 'star':8}]
for output1  in (map(lambda x : x['name'],l)):
    print('maping name:',output1)
for output2 in (map(lambda x : x['star']*10,l)):
    print('maping star:',output2)

Output

By using filter : [22, 54, 62]
maping name: includehelp
maping name: yash
maping name: sanjeev
maping star: 100
maping star: 80
maping star: 80





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.