Python provides different useful functions in order to help developers.
reduce() function is one of them where it is used to evaluate given list items with the provided function.
reduce() Function Syntax
reduce() function use the following syntax.
reduce(FUNCTION, SEQUENCE, INITIAL)
- `reduce` is the name reduce function
- `FUNCTION`is the function name we want to use for evaluation of the SEQUENCE items.
- `SEQUENCE` is the list that contains multiple items that are processed or evaluated by the FUNCTION.
- `INITIAL` can be set as the first argument to the FUNCTION but INITIAL is optional and generally not used.
Import functools Module/Library
In order to use the reduce() function we need to provide the module which provides it.
functools module provides the reduce() function so we will import functools like below.
reduce() Function Example
We will use the following example to explain the reduce() function. We will first import the reduce() function from the functools module. Then create a list of which name is
numbers and contains numbers. Then we will create a function that will be used inside the reduce() with the name of
my_sum(). Then we will call the reduce() function by providing the function name my_sum() and the list named numbers.
from functools import reduce numbers = [ 1 , 2 , 3 , 4 , 5 ] def my_sum(a,b): return a+b result = reduce(my_sum,numbers) print(result)
- For the first time, the first and second elements of the numbers list will be provided to the my_sum() function.
- As my_sum() function sum the given arguments which are 1 and 3 the function will return 3.
- Now the 3 and next which is third in the list number 3 will be provided to the my_sum() function which will sum them and return 6
- Now we have 6 and the fourth element of the numbers list which is 4 will be provided to the my_sum() function.
- … this will continue to every item in the list are evaluated with the given function and the last value will be set to the `result` variable.
reduce() Example with Initial Value
reduce() function generally used without an initial value which is described in the syntax part. But in some cases using initial value may be useful even necessary. by using initial value the first element will be the initial value and the provided list first element will be the second parameter.
from functools import reduce numbers = [ 1 , 2 , 3 , 4 , 5 ] def my_sum(a,b): return a+b result = reduce(my_sum,numbers,7) print(result)
- 7 is provided as the initial value and 1 is the first element of the numbers list. 7 and 1 will be provided to the function my_sum() as parameters and the result will be 8.
- In the second iteration, the 8 will be first and 2 will be the second argument to the function my_sum().
- … things will continue like normal reduce function.
reduce() Function with Operator Functions
Python provides the operator function which is simply 4 basic calculations like add, multiply, etc. We can use these operator functions with the reduce() function which will increase the code readability. In this example, we will use the add() operator function. In order to use operator functions, we should import the operator module.
from functools import reduce from operator import add numbers = [ 1 , 2 , 3 , 4 , 5 ] result = reduce(add,numbers) print(result)
We can see that we just provided the
reduce() with Lambda Function
Lambda function is used to create anonymous and inline functions without defining them explicitly. We can use reduce() and lambda functions together which will be more readable then explicit function definition. In this example, we will create a lambda function which will sum given list items.
from functools import reduce numbers = [ 1 , 2 , 3 , 4 , 5 ] result = reduce(lambda x,y: x+y ,numbers) print(result)
Convert List To String with reduce()
Another use case for the reduce is providing a string list and joining all items into a single string like a sentence. In this example, we will provide the list named
strlist to the reduce() function. We will also use the lambda operation where we will join given string elements together.
from functools import reduce from operator import add strlist=['I ', 'love ', 'poftut.com'] result = reduce(lambda x,y: x+y ,strlist) print(result)