We learned about anonymous functions, but let’s explore 3 of Python’s most powerful anonymous functions and these are map(), filter(), and reduce(). These 3 enable us to approach programming with a functional paradigm {which is another way of saying we use functions}.
Map
We can use the map() function to use all of the provided inputs within a function using. The map() function’s syntax is as follows:
- map(<function>, <list_of_inputs>)
Let’s consider an example to see where this can be useful:
Suppose we need to print the square of a list of items, and based on what we have learned we can approach this likewise:
items = [1, 2, 3, 4, 5] def sq_function(x): return x * x for item in items: value = sq_function(item) print(value)
This gives us:
The above approach while correct, is lengthy and with the map() function we can re-write the above code as shown below:
map_items = map(lambda x: x * x, [1, 2, 3, 4, 5]) for values in map_items: print(values)
Recall lambda functions? We can use lambda functions to create a function on the spot without having to use the ‘def’ keyword and here ‘x’ is the parameter.
Filter
We can use the filter() function to return a list of items based on whether or not a condition is true. The filter() function’s syntax is as follows:
- filter(<function >, <list_of_inputs>)
Consider an example where we can use this function, to return a list of numbers that are divisible by 5 within 5 to 56. A typical approach is:
number_list = list(range(5, 56)) def divisible_by_five(item): if item % 5 == 0: print(item) for number in number_list: divisible_by_five(number)
But we can simplify it using filter() as shown below:
number_list = list(range(5, 56)) divisible_by_five = filter(lambda x: x % 5 == 0, number_list) for numbers in divisible_by_five: print(numbers, end=" ")
This should give us:
This is similar to a for loop however filter is a built-in function and is faster.
NOTE: end=“ ” specifies that it should print the output separated by space instead of a newline.
Reduce
Reduce is different from the others in that instead of returning a list of items, it returns a singular value. The reduce() function’s syntax is as follows:
- reduce(<function>, <list_of_inputs>)
Consider an example where we want to print the sum of a list of numbers. We could write a simple program for this as shown below:
list_ = [2, 3, 4, 5, 6, 7] sum_ = 0 for item in list_: sum_ += item print("Sum is:", sum_)
But using reduce() we can re-write it as:
from functools import reduce list_ = [2, 3, 4, 5, 6, 7] sum_ = reduce(lambda a, b: a + b, list_) print("Sum is:", sum_)
This will give us:
We need to use the “functools” module to work with the reduce() function. Keep in mind, that “functools” is a built-in module so you do not have to install it.
What have we learned?
- What is the map() function?
- What is the filter() function?
- What is the reduce() function?
- Which keyword do we use for these 3 functions?
- How does the reduce() function differ from the map() & filter() function?
- Which built-in module has the reduce() functions?