[Tutor] Sum of List Elements

grouchy grouch at gmail.com
Sat Sep 24 17:52:52 CEST 2005


Also, the built in function sum():

total = sum(list)

Which is probably the One Obvious Way since 2.3, if I had to guess.

On 9/24/05, R. Alan Monroe < amonroe at columbus.rr.com> 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.
>
> > The best I have been able to do so far is to write a function that adds
> > list[0] and list[1], then list[1] and list [2], etc. Of course, this
> isn't
> > what I want.
>
> > I'd like to be able to sum a list of any size without having to type
> > list[0]+list[1]....
>
> total=0
> for x in list:
> total += x
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050924/44482405/attachment-0001.html


More information about the Tutor mailing list