# Python Lambda Functions with Examples

## What is the Lambda function?

While working with a complex system we need to divide the complex functions into small Lambda functions. For example, you are working on a complex calculator, one way is to put all the functions inside one function that is called when the user provides input and another way is to divide the function into many python functions. We can create separate functions of sum, subtract, divide, and multiplication.

Look at the function of the sum below.

```
def sum(x, y):
result = x + y
return result
```

The above code takes too much place. We can reduce it to one line code using lambda syntax.

The given below is the sum lambda function

`sum = lambda a, b: a + b`

```
def sum(x, y):
result = x + y
return result
print("normal function")
print(sum(5, 5))
print("lambda function")
sum = lambda a, b: a + b
print(sum(5, 5))
```

## Why the lambda function?

Whenever you a function for a shorter time period you can use lambda functions. Lambda functions are usually practiced when you require to pass a function to bigger functions. Some functions take functions as arguments there lambda functions are very useful.

## Example of function inside a function.

For example, we need a function that adds a number x and y. Y is in the programmer’s hands and needs to be changed with time and X is given by the user.

```
def add(y):
return lambda x : x + y
user = add(2)
print(user(5))
print(user(2))
```

Now the programmer need the function to add 2 and 5 at the same time.

```
def add(y):
return lambda x : x + y
user = add(2)
user1 = add(5)
print(user(5))
print(user1(5))
```

## Using the lambda function in the filter function

The filter function is used to filter the list or other data structure. The filter function takes a function and list as an argument.

```
numbers = [2, 3,7, 11, 14, 2, 6, 7, 17, 14, 0, 3, 21]
filtered_list = list(filter(lambda num: (num > 10), numbers))
print(filtered_list)
```

Here the lambda function filters the list.