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.

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 lambda keyword, followed by a list of arguments and a single expression. The syntax for a lambda function is as follows:

lambda arguments: expression

Here, arguments is a comma-separated list of arguments and expression is a single-line expression that is evaluated and returned by the lambda function.

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 x and y and returns their sum. We can use this lambda function just like any other function by calling it and passing the arguments.

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, filter, and reduce. These functions take a function as an argument and apply it to a sequence of elements.

Example of Lambda Function with map()

consider the following code that uses the map function to apply the lambda function to a list of numbers:

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

Output:

[1, 4, 9, 16, 25]

Here, the map function applies the lambda function lambda x: x**2 to each element of the numbers list and returns a new list of squared numbers.

Example of Lambda Function with filter()

We can also use lambda functions with the filter function to select elements that satisfy a certain condition. For example:

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 filter function applies the lambda function lambda x: x % 2 == 0 to each element of the numbers list and returns a new list containing only the even numbers.

Example of Lambda Function with reduce()

we can use the reduce function from the functools module to perform a reduction operation on a sequence of elements. For example:

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 reduce function applies the lambda function lambda x, y: x * y to the elements of the numbers 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.

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 sorted function to sort the list of students by their grades.

We pass a lambda function as the key argument to the sorted function that takes a single argument student and returns the grade of the student. The sorted function uses this lambda function to determine the sort order of the students.

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

Author

Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.

Write A Comment