[Tutor] Sum of List Elements

bob bgailer at alum.rpi.edu
Sat Sep 24 18:31:12 CEST 2005


At 08:11 AM 9/24/2005, Luke Jordan wrote:
>Hi All,
>
>I have spent an embarrassingly large amount of time trying to solve what 
>on its face seems like a simple problem.
>
>I have a list of intergers, and I want to assign the sum of the intergers 
>in the list to a variable. There are only intergers in the list.

In addition to the other solutions there is the (more generic?) use of the 
operator module and reduce function:

import operator
reduce(operator.add, (1,2,3))

Explore operator to see what other functions you can use.

Also there are the array handling modules such as numarray.

You can also define your own classes based on list and write methods to do 
these operations. 



More information about the Tutor mailing list