470,619 Members | 1,704 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,619 developers. It's quick & easy.

Re: sum up numbers in a list

sharon kim wrote:
i have a list, for example;
>>print L
['10', '15', '20']

is there a way to sum up all the numbers in a list? the number of
objects in the list is vary, around 50 to 60. all objects are 1 to 3
digit positive numbers.
the for-in statement is your friend:

S = 0
for item in L:
S += int(item)

it can also be used in-line (this is called a "generator expression"):

S = sum(int(v) for v in L)

or even hidden inside a built-in helper function:

S = sum(map(int, L))


Aug 26 '08 #1
0 1153

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by simpleman | last post: by
19 posts views Thread by Eduardo Bezerra | last post: by
11 posts views Thread by Leon | last post: by
60 posts views Thread by rhle.freak | last post: by
2 posts views Thread by Terry Olsen | last post: by
3 posts views Thread by djcamo | last post: by
33 posts views Thread by Andreas Prilop | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.