Javatpoint Logo
Javatpoint Logo

reduce() in Python

In Python, reduce() is a built-in function that applies a given function to the elements of an iterable, reducing them to a single value.

The syntax for reduce() is as follows:

  • The function argument is a function that takes two arguments and returns a single value. The first argument is the accumulated value, and the second argument is the current value from the iterable.
  • The iterable argument is the sequence of values to be reduced.
  • The optional initializer argument is used to provide an initial value for the accumulated result. If no initializer is specified, the first element of the iterable is used as the initial value.

Here's an example that demonstrates how to use reduce() to find the sum of a list of numbers:

Output:

Sum of the integers of num_list : 55
Sum of the integers of num_list with initial value 10 : 65

In this example, we use the reduce() function to apply an add function that returns the sum of two values to each pair of elements in the numbers list, resulting in the sum of all the elements.

Let us use the lambda function as the first argument of the reduce() function:

Let's break down how the reduce() function works for the example given:

The reduce() function takes two arguments: a function and an iterable. In this case, we use a lambda function lambda x, y: x * y as the function, and the numbers list as the iterable.

  • The lambda function accepts the variables x and y, multiplies them, and returns the outcome. When the lambda function is initially run, the variables x and y are set to the first and second elements, respectively, of the my_list (i.e., x = 1 and y = 2). The lambda function multiplies these two numbers and returns the result (1 * 2 = 2).
  • The second time the lambda function is called, x is set to the result of the previous call (x = 2), and y is set to the third element of the numbers list (i.e., y = 3). It multiplies these two values and returns the result (2 * 3 = 6).
  • The lambda function is repeatedly called in this way for each element in the my_list until all elements have been handled. The reduce() function returns the product of all the list's elements, which is then assigned to the product variable as 120. This product is calculated as follows: ((((1 * 2) * 3)* 4)* 5) = 120.
  • Finally, we print the value of the product variable using the print() function, which outputs 120.

reduce() function with operator functions

We can make our code more concise and easier to read by using operator functions instead of lambda functions.

Here is an example to demonstrate the use of operator functions as the first argument of reduce function:

Output:

Sum of all elements in my_list1 : 15
Product of all elements in my_list1 : 120
Concatenated string by using operator.concat : ILoveJavatpoint
Concatenated string by using operator.add : ILoveJavatpoint

This code shows how to use reduce() function and operator functions to perform mathematical and string operations on iterables in Python.

Understanding the different between reduce() and accumulate() functions:

The Python functools module provides the functions reduce() and accumulate(), both of which operate on iterables in ways that are comparable.

  1. The reduce() and accumulate() functions are similar in that they both accept two arguments: the iterable itself and a function that describes the operation to be carried out on it. The way they handle the operation's outcomes, however, is where they diverge most from one another.
  2. The reduce() function does the operation on the iterable's first two elements before running the same operation on the result and the next element. This process is repeated until all of the iterable's elements have been processed. The operation's final output is returned as a single result.
  3. While the accumulate() function also applies the operation to the iterable's first two elements before performing the same operation on the result and subsequent elements, the accumulate() function returns an iterator containing the operation's intermediate results. In other words, after each element has been handled, the accumulate() function provides a series of values representing the operation's outcome.

An example to understand the difference between accumulate() and reduce():

In this example, we have a list of numbers [1, 2, 3, 4, 5]. We use reduce() to calculate the product of all the numbers, which returns a single value of 120.

We also use accumulate() to calculate the product of all the numbers. However, instead of returning a single value, accumulate() returns an iterator that yields a sequence of intermediate results: [1, 2, 6, 24, 120].

So, the main difference between reduce() and accumulate() is that reduce() returns a single value that is the final output of the operation. In contrast, accumulate() returns an iterator that yields a sequence of intermediate results.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA