"Map, Filter, and Reduce" are three fundamental functions in Python that enable powerful data manipulation and transformation operations. These functions are essential tools in functional programming, a programming paradigm that focuses on using functions as the primary building blocks of software.
Functional programming is a programming paradigm that emphasizes the use of functions as the primary building blocks of software. It promotes immutability, higher-order functions, and the avoidance of side effects.
The map()
function takes two arguments: a function and an iterable. It applies the function to each item in the iterable.
# Example 1: Square each number in a list
numbers = [1, 2, 3, 4, 5]
squared = map(lambda x: x ** 2, numbers)
print(list(squared)) # Output: [1, 4, 9, 16, 25]
[1, 2, 3, 4, 5]
.map()
function to apply a lambda function to each number in the list. The lambda function takes a single argument x
and returns x ** 2
, which squares the number.map()
function returns an iterator, so we convert it to a list using list()
.You can also pass multiple iterables to map()
if the function takes multiple arguments.
# Example 2: Add corresponding elements of two lists
list1 = [1, 2, 3]
list2 = [4, 5, 6]
result = map(lambda x, y: x + y, list1, list2)
print(list(result)) # Output: [5, 7, 9]
list1
and list2
containing [1, 2, 3]
and [4, 5, 6]
, respectively.map()
function to apply a lambda function to pairs of corresponding elements from both lists. The lambda function takes two arguments x
and y
and returns their sum x + y
.map()
function returns an iterator, so we convert it to a list using list()
.The filter()
function takes two arguments: a function and an iterable. It returns an iterator that contains only the elements for which the function returns True.
# Example 3: Filter even numbers from a list
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = filter(lambda x: x % 2 == 0, numbers)
print(list(even_numbers)) # Output: [2, 4, 6, 8, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
.filter()
function to apply a lambda function to each number in the list. The lambda function checks if the number is even (x % 2 == 0
).filter()
function returns an iterator containing only the elements for which the lambda function returns True
, so we convert it to a list using list()
.You can use filter()
with a predefined function instead of a lambda function.
# Example 4: Filter names starting with 'A' from a list
names = ["Alice", "Bob", "Charlie", "Anna", "David"]
filtered_names = filter(lambda name: name.startswith('A'), names)
print(list(filtered_names)) # Output: ['Alice', 'Anna']
names
containing ["Alice", "Bob", "Charlie", "Anna", "David"]
.filter()
function to apply a lambda function to each name in the list. The lambda function checks if the name starts with the letter ‘A’ (name.startswith('A')
).filter()
function returns an iterator containing only the elements for which the lambda function returns True
, so we convert it to a list using list()
.The reduce()
function is part of the functools
module. It takes two arguments: a function and an iterable. It applies the function cumulatively to the items of the iterable, from left to right, to reduce it to a single value.
# Example 5: Sum of all elements in a list
from functools import reduce
numbers = [1, 2, 3, 4, 5]
total = reduce(lambda x, y: x + y, numbers)
print(total) # Output: 15
[1, 2, 3, 4, 5]
.reduce()
function from the functools
module.reduce()
function to apply a lambda function cumulatively to the numbers in the list. The lambda function takes two arguments x
and y
and returns their sum x + y
.reduce()
function reduces the list to a single value by applying the lambda function to pairs of elements from left to right.You can also provide an initial value to reduce()
.
# Example 6: Product of all elements in a list with an initial value
from functools import reduce
numbers = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, numbers, 1)
print(product) # Output: 120
[1, 2, 3, 4, 5]
.reduce()
function from the functools
module.reduce()
function to apply a lambda function cumulatively to the numbers in the list, starting with an initial value of 1
. The lambda function takes two arguments x
and y
and returns their product x * y
.reduce()
function reduces the list to a single value by applying the lambda function to pairs of elements from left to right.In the above topic, we've delved into the powerful functional programming tools provided by Python: map, filter, and reduce. These functions allow you to manipulate data in a concise and expressive manner, making your code more readable and efficient.
By mastering these concepts, you'll be able to write more elegant and maintainable code, leveraging the full power of functional programming paradigms in your Python projects. Experiment with the provided examples and explore further applications of map, filter, and reduce to deepen your understanding and enhance your programming skills. Happy coding! ❤️