In this exercise, we will review the
reduce(), higher-order functions provided by Python. These functions accept an iterable and a processing function as arguments and return another iterable. Similar to the exercise on lambdas, this section should serve as a refresher. You may recognize these functions from intermediate Python content. If you need more than a quick refresher and feel stuck at all during this exercise, we recommend you review this article as an aid.
The first function we will look at is
filter(), which converts the following imperative code:
nums = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) # filter_values is not a higher-order function def filter_values(predicate, lst): # Mutable list required because this example is imperative, not declarative ret =  for i in lst: if predicate(i): ret.append(i) return ret filtered_numbers = filter_values(lambda x: x % 2 == 0, nums) print(filtered_numbers) # This will output the list: [2, 4, 6, 8, 10]
into the following declarative code:
nums = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) filtered_numbers = filter(lambda x: x % 2 == 0, nums) print(tuple(filtered_numbers)) # This will output the tuple: (2, 4, 6, 8, 10)
map() converts the following imperative code:
nums = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) def mapper(function, lst): ret =  for i in lst: ret.append(function(i)) return ret mapped_numbers = mapper(lambda x: x*x, nums) print(tuple(mapped_numbers)) # This will output: (1, 4, 9, 16, 25, 36, 49, 64, 81, 100)
Into the following declarative code:
numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) mapped_numbers = map(lambda x: x*x, numbers) print(tuple(mapped_numbers)) # This will also output: (1, 4, 9, 16, 25, 36, 49, 64, 81, 100)
reduce() function accepts an iterable and a two-parameter function (no more than two) known as an *”accumulator”*. It uses the accumulator to recursively process the contents of the iterable and “reduce” them to one value. An example application of
reduce() would be to compute the sum of all numbers in a list.
The sum of all elements in a tuple written imperatively:
nums = (2, 6, 7, 9, 1, 4, 8) sum = 0 for i in nums: sum += i print(sum) # Output: 37
reduce() to find the sum declaratively:
""" In Python 3, the `reduce()` function has been moved to the `functools` library, so we need to import it before we can use it. """ from functools import reduce nums = (2, 6, 7, 9, 1, 4, 8) reduced_nums = reduce(lambda x, y: x + y, nums) # reduced_nums is a number print(reduced_nums) # Output: 37
The lambda in
reduce() requires exactly two parameters.
Higher-order functions are powerful tools that you can use to significantly shorten code, thereby leading to cleaner and more concise solutions. In later exercises, we will explore how to combine these functions.
filter() to remove odd numbers from tuple
nums. Print your result using the following line of code:
map() multiply all of the elements of
nums by 3. Print your result with the following line of code:
Find the sum of all the elements in the tuple
nums using the
reduce() function. Print your result with the following line of code: