Python Reduce Lambda Function

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

Program 1

# Reduce function in python
import functools 
# def addition(a,b):
#     return a+b
# # Calling 
# mylist=[1,2,3,4,5]
# result=functools.reduce(addition,mylist)
# print("Addition of Collection is: ",result)


# Reduce function with lambda

# calling
#mylist=[1,2,3,4,5]
mylist=[]
n=int(input("Enter the limit"))
print("Enter elements in collection: ")
for i in range(n):
    x=int(input())
    mylist.append(x)

result1=functools.reduce((lambda x,y:x+y),mylist)
result2=functools.reduce((lambda x,y:x-y),mylist)
result3=functools.reduce((lambda x,y:x*y),mylist)
result4=functools.reduce((lambda x,y:x//y),mylist)
print(result1)
print(result2)
print(result3)
print(result4)

 

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *