reduce() in python by R4R Team

- reduce() is a python function which apply on the particular function to manipulate the given sequence.
-It is defined in functools module.

Syntax-

functools.reduce(function,sequence)

-It return final value after iteration of the whole sequence through the function.


program-

#import the functools module
import functools

#define a function which return sum of two number
def f(x,y):
return x+y

#take input of the list
print("Enter number in list")
n=map(int,input().split())

x=functools.reduce(f,n)
print("Sum of list is :",x)


output-

Enter number in list
1 2 3 4
Sum of list is : 10


-In this program, we take an input of the list, and using reduce() function we find the sum of the list elements.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!