In this tutorial, we will learn about **reduce() function in Python**. The `reduce()`

Function in Python, which is accessible through the `functools`

module. It serves the purpose of repeatedly applying a specified function to the elements of a sequence. This iterative process leads to the gradual reduction of the sequence into a singular, consolidated value.

**RELATED POST: **Python Timeit Module

## reduce() function in Python

The `reduce()`

Function actively employs a binary function, one that takes two arguments, to iteratively process the elements of an iterable. This process occurs cumulatively, progressing from the leftmost element to the rightmost. As the iteration advances, the function is provided with both the accumulated outcome and the subsequent element of the iterable, which serve as its respective arguments.

### Syntax:

```
from functools import reduce
result = reduce(function, sequence[, initial_value])
```

`function`

: Choose the function that you wish to repeatedly employ, ensuring it accepts two arguments.`sequence`

: Select the iterable (e.g., list, tuple) on which you intend to apply the function cumulatively.`initial_value`

(optional): This acts as the initial argument for the function. If not provided, the first two elements of the iterable will serve as initial arguments.

## Different Examples of Python reduce() function

### Example: Calculate the sum of two numbers

```
from functools import reduce
# Defining a function to calculate the sum of two numbers
def add(x, y):
return x + y
numbers = [1, 2, 3, 4, 5]
# Utilizing reduce() to compute the sum of all numbers in the list
result = reduce(add, numbers)
print(result)
```

### Output:

`15 (1 + 2 + 3 + 4 + 5)`

In this illustrative scenario, the `add()`

function is iteratively executed across the elements of the `numbers`

list. This iterative process culminates in the computation of the summation of all the numbers. Importantly, it’s worth noting that starting from Python 3.8, the `reduce()`

function is no longer inherently integrated into Python and must be imported from the `functools`

module.

As a result, in situations involving Python 3.8 and subsequent versions, it becomes advisable to explore alternative techniques such as list comprehensions, loops, or other methodologies. This prudent approach arises from the consideration that the utilization of the `reduce()`

function might become less prevalent due to its less common usage in these later Python versions.

### Example: Calculating the Product of Numbers

```
from functools import reduce
def multiply(x, y):
return x * y
numbers = [2, 3, 4, 5]
result = reduce(multiply, numbers)
print(result)
```

### Output:

`120 (2 * 3 * 4 * 5)`

In this demonstration, we systematically use the `multiply()`

function on each element in the `numbers`

list. This gradual process accumulates results over time. To begin, the operation of the `reduce()`

function starts with the initial two elements, 2 and 3. These numbers undergo multiplication using the `multiply()`

function, resulting in a product of 6 (2 * 3 = 6). After this calculation, the obtained value of 6 gets combined with the subsequent element, which is 4, using the `multiply()`

function once more.

As a result, a new value of 24 emerges (6 * 4 = 24). This sequence of repeated steps continues, constantly merging each new element until the entire list is processed. Ultimately, these step-by-step computations culminate in determining the overall product of all numbers within the list, resulting in an output of 120.

### Example: Finding the Maximum Number

```
from functools import reduce
numbers = [12, 45, 23, 8, 50]
max_number = reduce(lambda x, y: x if x > y else y, numbers)
print(max_number)
```

### Output:

`50`

In this example, we’re using a special type of function called a lambda function for our reducing task. This lambda function has a simple job: it takes two numbers, compares them, and gives back the bigger one. Now, the `reduce()`

function takes this lambda function and walks through our list, putting it to work on pairs of numbers. After each comparison, it keeps the larger number. By repeating this process, the `reduce()`

function finally finds the largest number in the list, which in this case is 50.

### Example 4: Concatenating Strings

```
from functools import reduce
words = ['Developer', ' ', 'Helps', '!']
concatenated_string = reduce(lambda x, y: x + y, words)
print(concatenated_string)
```

### Output:

`Developer Helps!`

In this example, we use a special kind of function called a lambda function to put two strings together β let’s call them ‘x’ and ‘y’. Then, the `reduce()`

Function steps in and brings the strings from the list together, one after the other. This process leads us to the complete sentence ‘Hello world!’ as the outcome.