[Tutor] sum of a list

Alan Gauld alan.gauld at blueyonder.co.uk
Wed Aug 11 08:50:00 CEST 2004


> I can't figure out or find anywhere how to get the sum of a list?? 

L = [1,2,3]
sum = reduce(operator.add,L)

Which is shorthand for

sum = 0
for item in L:
   sum += item

Check my topic on Functional Programming for more info.

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld/tutor2/


More information about the Tutor mailing list