Lambda functions in Python are functions that are anonymous. They do not need to be named. In this post, we will see how to define and use lambda functions. We will also see the advantages of lambda functions over normal functions in Python.

### Defining a Lambda function in Python

Lambda function is just a fancy name for functions that don’t have a name. They can have multiple arguments, like any other function, but they can only have one expression.

To define a lambda function, we use the syntax as shown below:

`lambda argument(s): expression`

Let’s see an example.

1 2 3 | double = lambda n : n * 2 print(double(3)) |

Output:

`6`

We just wrote a lambda function to double the value passed as an argument.

Didn’t we just say that lambda function is anonymous and isn’t `double`

the name of the function here? However, it isn’t the case. Note that the statement `lambda n : n * 2`

is the whole function. We are assigning the function object to the variable `double`

. You could just as well write this:

1 | (lambda n : n * 2)(2) |

Also, when you inspect the lambda function:

1 2 | double = lambda n : n * 2 double |

Output:

`<function <lambda> at 0x7fb4cce3ae18>`

Did you notice that it shows `<lambda>`

instead of the function name?

Contrast that to the normal function define using `def`

.

1 2 3 4 | def double(n): return n * 2 double |

`<function double at 0x7fb4ccd7f8c8>`

### Multiple Arguments

Let’s see one more example with multiple arguments.

1 2 3 | multiply = lambda x, y : x * y print(multiply(3, 5)) |

Output:

`15`

When we want to pass multiple arguments, we join them with a comma.

### Advantages of Lambda functions over def functions

You might be wondering, if lambda function behaves the same way as def functions, why do I need to learn about lambda functions? However, there are some scenarios where lambda functions are more useful than the normal functions.

Following are some benefits of lambda functions:

- Lets you create a function inside a function
- Useful for functional programming, when used along with map(), reduce() and filter() function
- Can be used with sorted function to define an alternate sorting key.

Let us see some of the usage of lambda function.

### 1. Function inside a function

You can use a lambda function when you want to pass a function as an argument to another function. For example, consider a scenario when you want to multiply a number to itself for multiple times but don’t know the exact number. So, you could define a function this way:

1 2 | def power(num): return lambda x : x ** num |

Here, we are defining a function that calculates the value of a number when raised to the power of another number. Using this, we can define and use multiple lambda functions:

1 2 3 4 5 6 | # Using the same function 'power' to create multiple functions power_of_two = power(2) power_of_three = power(3) power_of_two(3) power_of_three(3) |

Output:

`9`

27

### 2. Map, Reduce and Filter

The real power of lambda functions are unleashed when used along with map, reduce and filter functions.

#### map() function

`Map()`

is used when you have a list of items on which a same kind of operation is to be performed. For example, when you have a list of items and you want to multiply all the items by 3. You can do this in a single line using map() and lambda function.

1 2 3 | my_list = [4,5,6] my_list = list(map(lambda x : x * 3, my_list)) my_list |

Output:

`[12, 15, 18]`

#### reduce() function

The` reduce()`

function comes in handy when you want to perform an operation on all the items in a list. Suppose, you want to add all the items in a list. Without a lambda function, the way you would do it is:

1 2 3 4 5 6 7 | list = [4,5,6] sum = 0 for i in list: sum = sum + i sum #Output = 15 |

However, using the `reduce()`

function, we can make it way simpler:

1 2 3 4 5 6 7 | from functools import reduce list = [4,5,6] sum = reduce((lambda x,y : x + y), list) sum #Output = 15 |

#### filter() function

The `filter()`

function is useful when you want to get the list of items for which a function returns `True`

. For example, you want to filter out the odd numbers from a list, you can do it this way:

1 2 3 4 5 | my_list = [4,5,6,7,7,9,10] odd_numbers = list(filter(lambda x: x % 2 != 0, my_list)) odd_numbers #Output = [5,7,7,9] |

### 3. Defining an alternate sorting key

Let’s say you have a list of names:

1 2 | name_list = ['Matt Broomhall', 'James Stewinsky', 'Elmo Jeong', 'Daren Mascia', 'Jason Mai'] |

You want to sort the names based on the first names:

1 | sorted(name_list) |

Output:

`['Daren Mascia', 'Elmo Jeong', 'James Stewinsky', 'Jason Mai', 'Matt Broomhall']`

Pretty easy? Now, you want to sort the list based on the last names? How would you do it? Lambda makes things easy here.

1 2 | sorted_by_last_name = sorted(name_list, key = lambda x : x.split()[1]) sorted_by_last_name |

Output:

`['Matt Broomhall', 'Elmo Jeong', 'Jason Mai', 'Daren Mascia', 'James Stewinsky']`

*See also*: Comprehensions in Python

*****

*If you have any questions or comments regarding the post (or
something else), please feel free to reach out through the comments.*

## Related posts

### Today's pick

### Categories

- Computer Vision/ML (3)
- Javascript (1)
- Linux (1)
- Python (20)
- Advance Python (3)
- Basic Python (6)
- Intermediate Python (11)

- Uncategorized (1)