## Python Lambda Function

## Definition

Python provides another way to create functions. This is how Python uses the Lambda keyword to create functions. A Lambda function can accept any number of arguments, evaluate an expression, and return a result. Results can be stored using variables.

### Syntax

lambda arguments : expression

### Example 1

x = lambda y : y * 2 print(x(5))

### Output

10

Here we create a lambda function, specifying only one argument as y, and the expression is y * 2.

Save the result to x.

Call the lambda function within the print statement with the value 5 tox as the argument.

### Example 2

x = lambda y,z :y * z + 2 print(x(5,10))

### Output

52

Here we have created a lambda function that takes two arguments y, z and returns the result of the expression y * z + 2.

### Example 3

a = lambda x,y,z : x + y * z + 2 print(a(5,10,12))

### Output

127

Here, we created a Lambda function that accepts three arguments and returns a result after evaluation.

There is no limit to the number of arguments, but you can create a Lambda function that can accept as many arguments as you want.

## Python Function Vs Python Lambda

# multiply two numbers using lambda a = lambda x,y: x*y print(a(3,10)) # outputs 30

# multiply two numbers using function def mul(x, y): return x*y print(mul(3, 10)) # outputs 30

## Lambda inside Functions

The power of Lambda functions comes in the form of anonymous functions in Python functions.

### Example 1

# function argument is x def mul(x): # lambda argument is y return lambda y: x * y # function argument value = 5 result = mul(5) # lambda argument value = 4 print(result(4))

### Output

20

### Example 2

# function argument is x def mul(x): # lambda argument is y return lambda y: x * y myList = [10, 20, 30, 40, 50] for i in myList: # function argument is i i.e. myList items result = mul(i) # lambda argument is 10 print(result(10))

### Output

100 200 300 400 500