Reduce () in Python

Python Methods and Functions

Works:

  • The first step selects the first two elements of the sequence and produces the result.
  • The next step is to apply the same function to earlier the result, and the number just follows the second element and the result is saved again.
  • This process continues until there are no more elements left in the container.
  • The final result is returned and printed on the console.

# Python code to demonstrate how the limit () method works

 
# import functools for the limit () method

import functools

 
# initializing list

lis = [ 1 , 3 , 5 , 6 , 2 ,]

 
# using the method Reduce the sum of the list

print ( "The sum of the list elements is:" , end = " ")

print (functools. reduce ( lambda a, b: a + b, lis))

 
# using Reduce to compute the maximum item from the list

print ( "The maximum element of the list is:" , end = "")

print (functools. reduce ( lambda a, b: a if a & gt; b else b, lis))

Output:

 The sum of the list elements is: 17 The maximum element of the list is: 6 

Using operator functions

Reduce () can also be combined with operator functions to achieve lambda-like functionality and make the code more readable.

# Python code to demonstrate how the limit () method works
# using operator functions

 
# import functools for the limit () method

import functools

 
# import statement for operator functions

import operator

 
# initializing list

lis = [ 1 , 3 , 5 , 6 , 2 ,]

 
# using the Reduce method to compute the list sum
# using operator functions

print ( "The sum of the list elements is:" , end = "")

print (functools. reduce (operator.add, lis))

 
# using Reduce to compute the product
# using operator functions

print ( "The product of list elements is: " , end = " ")

print (functools. reduce (operator.mul, lis))

 
# concatenate the string using a reducer

print ( "The concatenated product is:" , end = " ")

print (functools. reduce (operator.add, [ " geeks " , "for" , "geeks" ]))

Exit

 The sum of the list elements is: 17 The product of list elements is: 180 The concatenated product is: pythonengineering 

decrease () against accumulate ()

Both Reduce () and accumulate () can be used for you numeric summation of the elements of the sequence. But there are implementation differences in both of them.

  • Reduce () is defined in the functools module, accumulated () in the itertools module.
  • Reduce () stores the intermediate result and returns only the final summation value. Whereas, the accumulation () method returns a list containing intermediate results. The last number of the returned list is the sum of the list.
  • Reduce (Fun, Seq) takes a function as its first and a sequence as its second argument. Unlike accumulation (seq, fun) takes a sequence as 1st argument and a function as 2nd argument.

#code Python to demonstrate summation
# using Reduce () and accumulate ()

 
# imports itertools to accumulate ()

import itertools

 
# import functools for the limit () method

import functools

 
# initializing list

lis = [ 1 , 3 , 4 , 10 , 4 ]

 
# summing printing using accumulation ()

print ( "The summation of list using accumulate is:" , end = "")

print ( list (itertools.accumulate (lis, lambda x, y: x + y)))

 
< code class = "comments"> # summarize printing using Redu ()

print ( "The summation of list using reduce is:" , end = "")

print (functools. reduce ( lambda x, y: x + y, lis))

Output:

 The summation of list using accumulate is: [1, 4, 8, 18, 22] The summation of list using reduce is: 22 

This article courtesy of Manjeet Singh (S. Nandini) . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.





Get Solution for free from DataCamp guru