Applying 'Reduce' to a list
The following code will sum all the numbers in the list called 'a'.
sum = lambda a, b : a+b
a = [1 , 2 , 3 , 4 , 15]
from functools import reduce
b = reduce(sum, a)
print(b)
The following code will return the maximum number from a list of numbers;
mylst = [1,2,3,4,5,66,7,8,9]
from functools import reduce
b = reduce(max, mylst)
print(b)
Result = 66
Comments
Post a Comment
Write something to CodeWithAbdur!