×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Lambda Expression / Function in Python

By IncludeHelp Last updated : December 08, 2024

Python Lambda Function

In Python, there is a function named Lambda. The 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.

Creating a Lambda Function

To create a lambda function, you need to use the lambda keyword followed by the parameter_list and an expression. The parameter_list and expression must be separated with a colon (:). You can also assign the result to a variable.

Syntax to declare a lambda expression/function

Below is the syntax to declare a lambda expression/function:

lambda parameterlist : expression

Lambda Function "In Parameters"

The lambda expression is a short block of code which takes in parameters which are as follows:

  • 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.

Lambda Function Example

In this example, we are writing a lambda function to add two numbers. Add argument x with argument y, and return the result.

# Lambda function
result = lambda x, y: x + y

# Printing the result
print(result(10, 20))

The output would be:

30

Lambda Function: More Examples

Practice the below-given examples to understand the concept of lambda function:

Example 1: Simple Function Vs. Lambda Function

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.

Example 2: Lambda Function Different Forms

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(), and reduce() methods

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

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

The map() Method: It is 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: Lambda Function using the filter(), map(), and 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!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.