"reduce" in python

Home Forums Python Programming "reduce" in python

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #1960 Reply
    Humble
    Keymaster

    From the word itself we knows it reduces the result.. but , how much ? it reduce to a single value..The function reduce(func, seq) continually applies the function func() to the sequence seq and finally returns a single value.

    The seqence items will be taken in a pair and perform func operation and the result becomes the first arg again and next item in the sequence form the pair for next iteration of the func() and finally it returns the result which is a single value..

    >>>> l = range(1,100)
    >>> reduce(lambda x,y: x+y, l)
    4950
    >>> l = range(1,10)
    >>> reduce(lambda x,y: x+y, l)
    45
    >>> 
    
    
    hope its straightforward and requires no explanation further..
    
    A third argument can be passed to indicate the starting value.
    so that this  argument serve as a default result when the sequence is empty.
Viewing 1 post (of 1 total)
Reply To: "reduce" in python
Your information: