This is the last higher order function we'll see and like the previous ones it gets one function and one sequence.
-
The function takes two elements of the sequence at once.
-
It doesn't return a list, it returns a single value.
-
Must be imported.
from functools import reduce
Suppose we have the following list:
[2, 6, 9, 1]
The reduce function takes 2 values and performs an action, the result of this continues as the first parameter in the next execution and the same action is performed.
For this case we will add the values:
from functools import reduce
def add(num_a, num_b):
return num_a + num_b
numbers = [5, 9, 4, 1]
print(reduce(add, numbers))
# Output:
# 19
First of all
num_a
: 5num_b
: 9- returned value: 14
Then
num_a
: 14 (previous returned)num_b
: 4 (next in sequence)- returned value: 18
Finally
num_a
: 18num_b
: 1- Final returned value: 19
It's not the best way to do it, but it's a simple way to understand it.
Let's join the following list and return a string:
["My", "name", "is", "Esteban"]
from functools import reduce
def join_string(word_a, word_b):
string = f"{word_a} {word_b}"
return string
words = ["My", "name", "is", "Esteban"]
print(reduce(join_string, words))
# Output:
# My name is Esteban