---
title: "What is the use of a lambda function in Python? Provide an example."  
description: "What is the use of a lambda function in Python? Provide an example."  
author: "Utpal Vishwas"  
published: 2023-06-26  
updated: 2023-06-27  
canonical: https://www.mindstick.com/forum/158871/what-is-the-use-of-a-lambda-function-in-python-provide-an-example  
category: "python"  
tags: ["python"]  
reading_time: 2 minutes  

---

# What is the use of a lambda function in Python? Provide an example.

What is the use of a [lambda function](https://www.mindstick.com/forum/1967/lambda-function-using-unknown-parameter) in [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python)? Provide an example.

## Replies

### Reply by Aryan Kumar

A [lambda](https://www.mindstick.com/blog/181/lambda-expression-in-c-sharp) [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) is a small anonymous function. It is a way to define a function inline without having to give it a name. Lambda functions can be used anywhere that a regular function can be used, but they are especially useful in places where you only need to use the function once.

Here is an example of a lambda function:

Python

```plaintext
square = lambda x: x * x

print(square(5))
```

This code defines a lambda function called `square` that takes an argument `x` and returns the square of `x`. The lambda function is then called with the argument `5` and the result, 25, is printed to the console.

Here are some of the benefits of using lambda functions in Python:

- They can be used to define small, one-line functions.
- They can be used as arguments to other functions, such as `map()` and `filter()`.
- They can be used to make code more concise and readable.

Here are some examples of how lambda functions can be used in Python:

- To define a function that takes an argument and returns the square of that argument:

Python

```plaintext
square = lambda x: x * x
```

- To define a function that takes a list of numbers and returns a new list of the even numbers in the original list:

Python

```plaintext
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
```

- To define a function that takes a string and returns a new string with all the vowels removed:

Python

```plaintext
no_vowels = lambda text: ''.join(c for c in text if c not in 'aeiouAEIOU')
```

As you can see, lambda functions can be used to simplify and improve the readability of Python code. They are a powerful tool that can be used in a variety of ways.


---

Original Source: https://www.mindstick.com/forum/158871/what-is-the-use-of-a-lambda-function-in-python-provide-an-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
