ANONYMOUS FUNCTION IN PYTHON
=======================================================================================================
What is Anonymous Function?
➢ An anonymous function is a function that is defined without name
➢ In python, anonymous function are defined using the lambda keyword. Hence, anonymous
functions are also called lambda functions.
Lambda function use cases in Python
The Lambda function in Python can be used in the following situations:
• Small operations: the return value is calculated by evaluating an expression on a single line of
code. In this case, defining a complete function with def may seem too cumbersome.
• One-off operations: this means you don’t need to name the function, since it’s only used once. If
you need to repeat it or refer to it elsewhere in the same module, it’s best to use normal functions.
• Comparison of normal function and lambda function :
Lambda functions in Python are versatile and can be used in various scenarios. Here are some specific use
cases with examples and their outputs:
Condition Checking
Lambda functions can be used for quick, inline condition checking.
Example:
is_even = lambda x: x % 2 == 0
print(is_even(4))
print(is_even(5))
Output:
True
False
Python Lambda Function with List Comprehension
They can be combined with list comprehension to apply a function to every element in a list.
Example:
squares = [(lambda x: x*x)(x) for x in range(10)]
print(squares)
Output:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Python Lambda Function with If-Else
Lambda functions can also incorporate simple if-else conditions within them.
Example:
max_number = lambda x, y: x if x > y else y
print(max_number(5, 7))
print(max_number(10, 3))
Output:
7
10
Python Lambda Function with Multiple Statements
While lambda functions are limited to a single expression, you can simulate multiple operations with
logical operators or conditional expressions.
Example:
func = lambda x: (x := x + 2, x * x)
print(func(5))
Output:
49
How to Use Lambda Function with filter() -
The filter() function in Python takes in a function and a list as arguments. That function is called with every
item in the list, and a new list is returned that contains items for which the function evaluates to True.
The following example uses the filter function to filter out numbers greater than 10:
Example:
numbers = [4, 1, 7, 3, 6, 12, 18, 16, 50, 10, 4, 5]
numbers_gt_10 = list(filter(lambda x: x > 10, numbers))
print(numbers_gt_10)
Output:
[12, 18, 16, 50]
Using Lambda Function in Python with map()
The map() function in Python also takes in a function and a list as arguments. That function is called for all
the items in that list, and the latest list is returned, that contains items returned by that function.
The following example uses the map function to double all the numbers in a list:
Example:
numbers = [1, 2, 3, 4, 5, 6]
doubles = list(map(lambda x: x * 2, numbers))
print(doubles)
Output:
[2, 4, 6, 8, 10, 12]
Using Lambda Function in Python with reduce()
The reduce() function in Python, which is part of the functools module, is a powerful tool often used with
lambda functions. It applies a function of two arguments cumulatively to the items of an iterable, reducing the
iterable to a single value. When used with lambda functions, reduce() becomes even more versatile.
Here's how you can use a lambda function with reduce():
Example 1: Sum of a List
from functools import reduce
numbers = [1, 2, 3, 4, 5]
sum_numbers = reduce(lambda x, y: x + y, numbers)
print(sum_numbers)
Output:
15
Example 2: Finding Maximum in a List
max_number = reduce(lambda x, y: x if x > y else y, numbers)
print(max_number)
Output:
Example 3: Concatenating Strings
strings = ['Python', 'Lambda', 'Function']
concatenated_string = reduce(lambda x, y: x + " " + y, strings)
print(concatenated_string)
Output:
'Python Lambda Function'