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

Popular posts from this blog

Quotation marks to wrap an element in HTML

The Basic Structure of a Full-Stack Web App

Unlocking Web Design: A Guide to Mastering CSS Layout Modes