Python reduce() Function Tutorial with Examples – POFTUT

Python reduce() Function Tutorial with Examples

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 is the name reduce function
  • FUNCTIONis 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.

reduce() Function Example

reduce() Function Example

  1. For the first time, the first and second elements of the numbers list will be provided to the my_sum() function.
  2. As my_sum() function sum the given arguments which are 1 and 3 the function will return 3.
  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
  4. Now we have 6 and the fourth element of the numbers list which is 4 will be provided to the my_sum() function.
  5. … 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.
LEARN MORE  Python Lambda - Anonymous Function Tutorial with Examples

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.

reduce() Example with Initial Value

reduce() Example with Initial Value

  1.  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.
  2. In the second iteration, the 8 will be first and 2 will be the second argument to the function my_sum().
  3. … 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.

reduce() Function with Operator Functions

reduce() Function with Operator Functions

We can see that we just provided the add() function.

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.

LEARN MORE  Python Lambda - Anonymous Function Tutorial with Examples

reduce() with Lambda Function

reduce() with Lambda Function

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.

Convert List To String with reduce()

Convert List To String with reduce()

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)