Lambda Functions in Python

About Lambda Functions

In Python, a lambda function is a small anonymous function that can take any number of arguments but can have only one expression.

It is often used for short, throwaway functions that are not meant to be reused elsewhere in the code.

lambda keyword is used to define a lambda function.

Lambda function returns the value of the expression automatically. It does not require a return statement.

Let's compare a normal function and a lambda function that performs the same operation of adding two numbers:

Normal Function Example
Lambda Function Example

Ways of Writing Lambda in Python

1. Lambda function with single argument

A lambda function can be defined with a single argument. For example, the following lambda function takes one argument and returns its square:

2. Lambda function with multiple arguments

A lambda function can also take multiple arguments. For example, the following lambda function takes two arguments and returns their sum:

3. Lambda function with map() function

Lambda functions are often used in conjunction with the map() function to apply a function to each item in an iterable. For example, the following code uses a lambda function to square each number in a list:

map(lambda x: x ** 2, numbers) applies the lambda function to each item in the numbers list, returning a new list of squared numbers.

4. Lambda function with filter() function

Lambda functions are often used in conjunction with the filter() function to filter items in an iterable. For example, the following code uses a lambda function to filter out even numbers from a list:

filter(lambda x: x % 2 == 0, numbers) applies the lambda function to each item in the numbers list, returning a new list of even numbers.

5. Lambda function with reduce() function

Lambda functions are often used in conjunction with the reduce() function to apply a rolling computation to sequential pairs of values in a list. For example, the following code uses a lambda function to compute the product of all numbers in a list:

reduce(lambda x, y: x * y, numbers) applies the lambda function to each item in the numbers list, returning the product of all numbers.

6. Lambda function with conditional expression

Lambda functions can also include conditional expressions. For example, the following lambda function returns the maximum of two numbers:

max_num = lambda x, y: x if x > y else y defines a lambda function that returns the maximum of two numbers.

The following lambda function returns the string "Even" if a number is even, and "Odd" if it is odd:

check = lambda x: "Even" if x % 2 == 0 else "Odd" defines a lambda function that checks if a number is even or odd.

7. Lambda function with sorted function

Lambda functions can be used with the sorted() function to sort a list based on a custom key. For example, the following code processes a list of tuples containing student names and their scores, and sorts them based on their scores in descending order:

sorted(students, key=lambda x: x[1], reverse=True) sorts the students list based on the second element (the score) of each tuple in descending order.


Note: While lambda functions can be useful for short, simple functions, they can also make code harder to read and understand if overused or used inappropriately. It is generally recommended to use regular function definitions for more complex functions or when code readability is a priority.

Python Compiler For Live Practice

Practice the above Python code in the following Python Editor. Create more lambda functions to test your understanding in the following code editor and click Execute button. Output will be displayed in Python Code Output section.

Python logo Python Editor
Python Code Output