Lambda Functions in Python

Updated: April 18th, 2023, 11:25:19 IST
Published: April 18th, 2023
Lambda Functions in Python
Title: Lambda Functions in Python

A lambda function is an anonymous function in Python that can have any number of arguments, but can only have one expression. It is defined using the keyword lambda followed by a comma-separated list of arguments and the expression.

Python is a popular programming language that has a rich set of features and functionalities. One of the interesting features of Python is lambda functions. In this blog, we will discuss Python lambda functions with examples.

Lambda Functions in Python: Basic Syntax

The syntax for lambda function is as follows:

lambda arguments: expression

Example 1: Simple Lambda Function

Let's start with a simple lambda function that takes a single argument and returns its square.

square = lambda x: x**2
print(square(5)) # Output: 25

In the above example, we defined a lambda function square that takes an argument x and returns its square. We then called the lambda function with the argument 5 and printed the result.

Example 2: We can also write lambda function with multiple arguments

Lambda functions can also take multiple arguments. Let's see an example of a lambda function that takes two arguments and returns their sum.

sum = lambda x, y: x + y
print(sum(10, 20)) # Output: 30

In the above example, we defined a lambda function sum that takes two arguments x and y and returns their sum. We then called the lambda function with the arguments 10 and 20 and printed the result.

Example 3: Lambda Function with Built-in Function

Lambda functions can be used with built-in functions like map, filter, and reduce. Let's see an example of a lambda function used with the map function.

numbers = [1, 2, 3, 4, 5]
squares = list(map(lambda x: x**2, numbers))
print(squares) # Output: [1, 4, 9, 16, 25]

In the above example, we defined a list of numbers and used the map function to apply a lambda function that takes a number and returns its square. The map function applies the lambda function to each element of the list and returns a new list containing the results.

Example 4: Sorting a List using Lambda Function

Lambda functions can also be used to sort a list of elements. Let's see an example of a lambda function used with the sorted function.

fruits = ['apple', 'banana', 'cherry', 'durian']
sorted_fruits = sorted(fruits, key=lambda x: len(x))
print(sorted_fruits) # Output: ['apple', 'durian', 'banana', 'cherry']

In the above example, we defined a list of fruits and used the sorted function to sort them based on their length. We passed a lambda function as the key argument to the sorted function that takes a fruit name and returns its length. The sorted function sorts the list based on the length of each fruit name.

Conclusion

Lambda functions in Python are a powerful tool that can be used to create small, anonymous functions that can be used with other functions or methods. They are easy to define and can take any number of arguments. In this blog, we discussed the syntax of lambda functions and provided some examples of their usage.