Python reduce() Function
The function reduce(function_name, seq) continually applies the function function_name() to the sequence seq. It returns a single value.
If seq = [ a1, a2, a3, … , an ], calling reduce(func, seq) works like this: At first the first two elements of seq will be applied to func, i.e. function_name(a1,a2) The list on which reduce() works looks now like this: [ function_name(a1, a2), a3, … , an ] In the next step func will be applied on the previous result and the third element of the list, i.e. func(func(a1, a2),a3) The list looks like this now: [ function_name(function_name(a1, a2),a3), … , an ] Continue like this until just one element is left and return this element as the result of reduce() We illustrate this process in the following example:
Follow me on linkedin.com.
https://www.linkedin.com/in/ankurjavaarch