Thursday, 22 February 2018

reduce function in python

reduce( ) support only python 2.x

reduce( ) doesn't support python 3.x

Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value. 

For example, 
reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) 

calculates ((((1+2)+3)+4)+5). 

The left argument - x is the accumulated value and the right argument - y is the update value from the iterable object.

x=[2,4,5,6,3]
y=reduce(lambda i,j:i+j,x)

print(y)

20

1 comment:

  1. Great explanation about reduce function in python. We are technology/news/smartphone company, If you want to read such useful news then, Visit us: https://techmie.com/

    ReplyDelete