Python lambda – Anonymous Function

  • Python lambda function or Python anonymous function has no name.
  • We can define an anonymous function using the lambda reserved keyword.
  • The anonymous function scope is limited to the current scope where it’s defined.
  • A lambda function can have one or more arguments but it can have only one expression.
  • The expression is evaluated and the result is returned from the lambda function.
  • The lambda functions are commonly used with map(), filter(), and reduce() operations.

Python lambda Function Syntax

The lambda function syntax is:

lambda arguments : expression

Python Anonymous Function Example

Let’s say we have a function to get the area of a rectangle.

def area_of_rectangle(l, w):
    return l * w


print(f'Area of Rectangle (4, 5) is {area_of_rectangle(4, 5)}')

Let’s create an anonymous function using the lambda keyword to get the area of the rectangle.

rectangle_area = lambda x, y: x * y

print(f'Area of Rectangle (4, 5) is {rectangle_area(4, 5)}')

When to use Anonymous Function?

  • For small trivial tasks with not much complexity.
  • When the function has only a single expression.
  • For repetitive tasks that are temporary in nature.
  • When you want the function scope to be limited to the current scope only.
  • It’s useful when a function argument is another function such as map(), filter(), and reduce() functions.

Lambda Function with map()

The map() function takes a function and an iterable as the arguments. The function is applied to every element in the iterable and the updated iterable is returned.

Let’s say we have a list of integers. We have to create a new list by multiplying every element with 10. We can use lambda function here rather than creating a function for this single use case.

list_numbers = [1, 2, 3, 4]

list_numbers = map(lambda x: x * 10, list_numbers)

for num in list_numbers:
    print(num, end=" ")

Output:

Python Lambda Function With Map
Python lambda Function with map()

Lambda Function with filter()

The built-in filter() function takes a function and an iterable as the argument. The function is applied to each element of the iterable. If the function returns True, the element is added to the returned iterable.

Let’s say we have a list of integers and we want to remove all the odd numbers. The final list should have only even integers. We can use filter() function here with the lambda function.

list_numbers = [1, 2, 3, 4, 5, 6]

list_numbers = filter(lambda x: x % 2 == 0, list_numbers)

for num in list_numbers:
    print(num, end=" ")
Python Lambda Function With Filter
Python lambda Function with filter()

Lambda Function with reduce()

The reduce() function is present in the functools module. This function takes a function and a sequence as the argument. The function should accept two arguments. The elements from the sequence are passed to the function along with the cumulative value. The final result is a single value.

Let’s say we have a list of integers and we want to get the sum of all of its elements. We can use the reduce() function here with the lambda function.

from functools import reduce

list_ints = [1, 2, 3, 4, 5, 6]

total = reduce(lambda x, y: x + y, list_ints)

print(f'Sum of list_ints elements is {total}')
Python Lambda Function With Reduce
Python lambda Function with reduce()

Python lambda function with no arguments

Someone asked me if we can have a lambda function without any argument?

Yes, we can define a lambda function without any argument. But, it will be useless because there will be nothing to operate on. Let’s have a look at a simple example.

get_5 = lambda: 5

print(get_5())  # 5

Since the lambda function is always returning the same value, we can just assign it a variable. Using lambda function without any argument is plain abuse of this feature.


Conclusion

Python anonymous functions are created using the lambda keyword. They are useful when a function argument is another function. It’s mostly used to create simple utility functions for one-time use.


References: