473,385 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Re: sum up numbers in a list

sharon kim wrote:
i have a list, for example;
>>L=[]
>>L.append('10')
>>L.append('15')
>>L.append('20')
>>len(L)
3
>>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))

</F>

Aug 26 '08 #1
0 1242

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: simpleman | last post by:
Hi, I have an assignment that requires me to subtract two very large unsigned integers using linked list.AFAIK I have to manipulate data as character. But I dont know how to get input into the...
19
by: Eduardo Bezerra | last post by:
Hi, I'm looking for an efficient way to create the oposite of a list of numbers. For example, suppose I have this list of numbers: 100 200 300
11
by: Leon | last post by:
I have six textbox controls on my webform that allows the user to enter any numbers from 1 to 25 in any order. However, I would like to sort those numbers from least to greatest before sending them...
1
by: carlg | last post by:
There seem to be plenty of Javascript implemented WYSIWYG editors for editing rich text in a web browser, but I haven't been able to find one that shows line numbers. Even if that was the only...
60
by: rhle.freak | last post by:
Here is my code to generate prime numbers.It works absolutely fine when the range is *not very large*. However on initializing i with a large integer it produces erroneous results (some numbers...
2
blyxx86
by: blyxx86 | last post by:
Great insight needed!! Good evening everyone.. I've come to a point where I can't even fathom how to go forward. I'm attempting to create a serialized inventory. I don't know where to begin....
2
by: Terry Olsen | last post by:
I have an array filled with counts of numbers. Example: ar(1) contains the number of occurrances of the number 1 ar(2) contains the number of occurrances of the number 2 etc... I need to find...
3
by: djcamo | last post by:
Hi, I have a situation where I have a collection that holds numbers. Mostly they are concurrent eg. 125801-125899 but sometimes they are not eg. 125801-125899, 195301-399. Is there any way to...
33
by: Andreas Prilop | last post by:
To get bold numbers in ordered lists, one can write ol { font-weight: bold } ol span { font-weight: normal } <ol> <li><span>......</span></li> <li><span>......</span></li> </ol>
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.