Lambda, Map, Filter, and Reduce in Python
Lambda
The Lambda function, also known as an anonymous or inline function, is a way to create a function without giving it a name. This can be useful when you need to define a function that will only be used once, or when you want to pass a function as an argument to another function.
Here is an example of using a Lambda function in Python:
1# Define a Lambda function that takes two arguments and returns their sum
2sum_func = lambda x, y: x + y
3
4# Call the Lambda function
5result = sum_func(1, 2) # Returns 3
Map
The Map function in Python applies a function to each element in a sequence of data. This can be useful for transforming the elements in a sequence, such as converting a list of strings to a list of integers or a list of tuples to a list of lists.
Here is an example of using the Map function in Python:
1# Define a function that converts a string to an integer
2def string_to_int(s):
3 return int(s)
4
5# Define a list of strings
6strings = ['1', '2', '3', '4', '5']
7
8# Apply the string_to_int function to each element in the list using Map
9integers = list(map(string_to_int, strings)) # Returns [1, 2, 3, 4, 5]
In this example, we first define a function that takes a string as input and returns the corresponding integer value. Then we define a list of strings and use the map
function to apply the string_to_int
function to each element in the list. The map
function returns a generator object, which we can convert to a list using the list
function.
The Map function can also be used with Lambda functions to create a more concise and expressive syntax. Here is an example of using a Lambda function with Map:
1# Define a list of strings
2strings = ['1', '2', '3', '4', '5']
3
4# Use Map and a Lambda function to convert the strings to integers
5integers = list(map(lambda s: int(s), strings)) # Returns [1, 2, 3, 4, 5]
In this example, we use the map
function to apply a Lambda function to each element in the list of strings. The Lambda function takes a string as input and returns the corresponding integer value. The map
function returns a generator object, which we can convert to a list using the list
function.
Filter
The Filter function in Python is used to select elements from a sequence of data based on a certain criterion. This can be useful for selecting only the elements that meet a certain condition, such as selecting only the even numbers from a list of integers.
Here is an example of using the Filter function in Python:
1# Define a function that checks if a number is even
2def is_even(n):
3 return n % 2 == 0
4
5# Define a list of numbers
6numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
7
8# Use Filter and the is_even function to select the even numbers from the list
9even_numbers = list(filter(is_even, numbers)) # Returns [2, 4, 6, 8, 10]
In this example, we first define a function that takes a number as input and returns True
if the number is even and False
otherwise. Then we define a list of numbers and use the filter
function to apply the is_even
function to each element in the list. The filter
function returns a generator object that contains only the elements for which the is_even
function returned True
. We can convert the generator object to a list using the list
function.
The Filter function can also be used with Lambda functions to create a more concise and expressive syntax. Here is an example of using a Lambda function with Filter:
1# Define a list of numbers
2numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
3
4# Use Filter and a Lambda function to select the even numbers from the list
5even_numbers = list(filter(lambda n: n % 2 == 0, numbers)) # Returns [2, 4, 6, 8, 10]
In this example, we use the filter
function to apply a Lambda function to each element in the list of numbers. The Lambda function takes a number as input and returns True
if the number is even and False
otherwise. The filter
function returns a generator object that contains only the elements for which the Lambda function returned True
. We can convert the generator object to a list using the list
function.
Reduce
The Reduce function in Python is used to apply a function to a sequence of data and reduce the sequence to a single value. This can be useful for combining the elements in a sequence in some way, such as computing the sum or product of a list of numbers.
Here is an example of using the Reduce function in Python:
1from functools import reduce
2
3# Define a function that computes the product of two numbers
4def product(x, y):
5 return x * y
6
7# Define a list of numbers
8numbers = [1, 2, 3, 4, 5]
9
10# Use Reduce and the product function to compute the product of all the numbers in the list
11result = reduce(product, numbers) # Returns 120
In this example, we first define a function that takes two numbers as input and returns their product. Then we define a list of numbers and use the reduce
function to apply the product
function to each element in the list. The reduce
function starts by applying the product
function to the first two elements in the list, then applies the product
function to the result of that operation and the next element in the list, and so on, until all the elements in the list have been combined. The final result is a single value that is the result of combining all the elements in the list using the product
function.
The Reduce function can also be used with Lambda functions to create a more concise and expressive syntax. Here is an example of using a Lambda function with Reduce:
1# Define a list of numbers
2numbers = [1, 2, 3, 4, 5]
3
4# Use Reduce and a Lambda function to compute the product of all the numbers in the list
5result = reduce(lambda x, y: x * y, numbers) # Returns 120
In this example, we use the reduce
function to apply a Lambda function to each element in the list of numbers. The Lambda function takes two numbers as input and returns their product. The reduce
function starts by applying the Lambda function to the first two elements in the list, then applies the Lambda function to the result of that operation and the next element in the list, and so on, until all the elements in the list have been combined. The final result is a single value that is the result of combining all the elements in the list using the Lambda function.
Using Lambda, Map, Filter, and Reduce All Together
Here is an example of using the Lambda, Map, Filter, and Reduce functions together in Python:
1from functools import reduce
2
3strings = ['1', '2', '3', '4', '5']
4
5# Use Map and a Lambda function to convert the strings to integers
6integers = list(map(lambda s: int(s), strings)) # Returns [1, 2, 3, 4, 5]
7
8# Use Filter and a Lambda function to select only the even numbers
9even_numbers = list(filter(lambda n: n % 2 == 0, integers)) # Returns [2, 4]
10
11# Use Reduce and a Lambda function to compute the sum of the even numbers
12result = reduce(lambda x, y: x + y, even_numbers)
Author: Sadman Kabir Soumik