In this post, we will learn about what is lambda functions, syntax of the lambda function, uses of the lambda function, and more with examples and explanations so let’s start learning from the very basics.

**Table of Contents**hide

## What is Lambda Function?

Python lambda function is a small anonymous function without a name. It can take any number of arguments but only have one expression.

Lambda functions are often used as a shortcut for a function that is only used once.

## Syntax of Lambda Function

A lambda function is defined using the

keyword, followed by a list of arguments and a single expression. The syntax for a lambda function is as follows:**lambda**

`lambda arguments: expression`

Here,

is a comma-separated list of arguments and **arguments**

is a single-line expression that is evaluated and returned by the lambda function.**expression**

For example, consider the following lambda function that adds two numbers:

```
add = lambda x, y: x + y
result = add(10, 20)
print(result)
```

**Output:**

30 |

This lambda function takes two arguments

and **x**

and returns their sum. We can use this lambda function just like any other function by calling it and passing the arguments.**y**

## Example for Practice of Lambda Function

**Example-1:** Define a lambda function that calculates the square of a number

```
# Define a lambda function that calculates the square of a number
square = lambda x: x**2
# Call the lambda function and print the result
print(square(5))
print(square(10))
```

**Output:**

25 100 |

**Example-2:** Define a lambda function that calculates the factorial of a number

```
# Define a lambda function that calculates the factorial of a number
factorial = lambda n: 1 if n == 0 else n * factorial(n - 1)
# Call the lambda function and print the result
print(factorial(5))
print(factorial(10))
```

**Output:**

120 3628800 |

## Uses of Lambda Function

Generally, Lambda functions are used with higher-order functions such as

,**map**` `

, and **filter**

. These functions take a function as an argument and apply it to a sequence of elements.**reduce**

### Example of Lambda Function with map()

consider the following code that uses the

function to apply the lambda function to a list of numbers:**map**

```
numbers = [1, 2, 3, 4, 5]
squares = map(lambda x: x**2, numbers)
print(list(squares))
```

**Output:**

[1, 4, 9, 16, 25] |

Here, the

function applies the lambda function **map**

to each element of the **lambda x: x**2**

list and returns a new list of squared numbers.**numbers**

### Example of Lambda Function with filter()

We can also use lambda functions with the

function to select elements that satisfy a certain condition. For example:**filter**

```
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
evens = filter(lambda x: x % 2 == 0, numbers)
print(list(evens))
```

**Output:**

[2, 4, 6, 8, 10] |

In this case, the

function applies the lambda function **filter**

to each element of the **lambda x: x % 2 == 0**

list and returns a new list containing only the even numbers.**numbers**

### Example of Lambda Function with reduce()

we can use the

function from the **reduce**

module to perform a reduction operation on a sequence of elements. For example:**functools**

```
from functools import reduce
numbers = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, numbers)
print(product)
```

**Output:**

120 |

In this case, the

function applies the lambda function **reduce**

to the elements of the **lambda x, y: x * y**

list in a cumulative manner, starting with the first two elements and then the result of that operation and the next element, and so on. The final result is the product of all the elements in the list.**numbers**

### Example of Lambda Function with sorted()

```
# Define a list of tuples representing students and their grades
students = [('Alice', 90), ('Bob', 80), ('Charlie', 70), ('David', 60), ('Eve', 50)]
# Sort the list of students by their grades using a lambda function as the key
sorted_students = sorted(students, key=lambda student: student[1])
# Print the result
print(sorted_students)
```

**Output:**

[(‘Eve’, 50), (‘David’, 60), (‘Charlie’, 70), (‘Bob’, 80), (‘Alice’, 90)] |

In this example, we define a list of tuples representing students and their grades and then use the

function to sort the list of students by their grades.**sorted**

We pass a lambda function as the

argument to the **key**

function that takes a single argument **sorted**

and returns the grade of the student. The **student**

function uses this lambda function to determine the sort order of the students.**sorted**

That’s all I hope this article has helped you understand Python Lambda functions.

## 2 Comments

Pingback: Filter odd and even numbers from the list in python – allinpython.com

Pingback: Map() and Filter() Function in Python – allinpython.com