Lambdas, Map, and Filter in Python: Functional Programming in ActionAn Overview of Anonymous Functions and Functional ProgrammingExploring Lambdas, Map, and Filter in Python: Functional Programming in Action
In Python, a lambda function (also called an anonymous function) is a function that doesn’t have a name. Normally, functions are defined using the def
keyword, but lambda functions are defined using the lambda
keyword.
Syntax Comparison: Lambda Function vs Regular Function
Lambda Function Syntax:
lambda argument: expression
Regular Function Syntax:
def function_name(argument):
return expression
Examples of Lambda Functions
- Sum of Two Numbers with Lambda Function:
sum_of_two_number = lambda x, y: x + y
print(sum_of_two_number(10, 20))
Output:
30
- Multiplication of Two Numbers with Lambda Function:
multiplication_of_two_number = lambda x, y: x * y
print(multiplication_of_two_number(10, 10))
Output:
100
Explanation:
In the above examples, sum_of_two_number
and multiplication_of_two_number
These are variables that store the result from the lambda function. lambda
is the keyword used to define the anonymous function. x
and y
are the function’s arguments, and the expression (x + y
or x * y
) is the result.
Common Functional Programming Functions in Python
1. map() Function
The map()
Function applies a function to each item in an iterable (like a list). It returns an iterable object, which is usually converted into a list.
- Syntax:
map(function, iterable)
You can use both regular functions and lambda functions with map()
. Lambda functions are often used for a more Pythonic approach.
With Regular Function:
def sqr(x):
return x * x
lst_of_digits = [1, 2, 3, 4, 5]
sqr_lst = list(map(sqr, lst_of_digits))
print(sqr_lst)
Output:
[1, 4, 9, 16, 25]
With Lambda Function:
sqr_lst = list(map(lambda item: item * item, [1, 2, 3, 4, 5]))
print(sqr_lst)
Output:
[1, 4, 9, 16, 25]
2. filter() Function
The filter()
function filters items from an iterable based on a function that returns True
or False
. It returns only the items for which the function returns True
.
- Syntax:
filter(function, iterable)
Without Lambda (Find Even Numbers):
def is_even(x):
return x % 2 == 0
numbers = [2, 5, 4, 22, 13, 122, 34, 25, 21]
even_numbers = list(filter(is_even, numbers))
print(even_numbers)
Output:
[2, 4, 22, 122, 34]
With Lambda (Find Even Numbers):
even_numbers = list(filter(lambda x: x % 2 == 0, [2, 5, 4, 22, 13, 122, 34, 25, 21]))
print(even_numbers)
Output:
[2, 4, 22, 122, 34]
3. reduce() Function
The reduce()
Function applies a function cumulatively to the items in an iterable, reducing it to a single result. To use reduce()
, you must import it from the functools
module.
- Syntax:
from functools import reduce
reduce(function, iterable)
Example (Sum of Numbers):
from functools import reduce
def add(x, y):
return x + y
numbers = [1, 2, 3, 4, 5, 6]
total = reduce(add, numbers)
print(total)
Output:
CopyEdit21
Conclusion
Lambda functions provide a concise way to define small functions for simple tasks. They are especially useful when combined with functional programming tools like map()
, filter()
, and reduce()
, helping to write cleaner, more Pythonic code.
Leave a Reply