| | |
adding list items
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 33
Reputation:
Solved Threads: 0
Okay, sorry, I just posted a different thread here a couple days ago, I know. But I have another question and I've googled it and can't find the answer.
let's say I have a list
num=[1,2,3,4,5,6]
and I want to add all of the items together for one big number. (In this case- 21.) How would I go about doing that?
let's say I have a list
num=[1,2,3,4,5,6]
and I want to add all of the items together for one big number. (In this case- 21.) How would I go about doing that?
Use the
reduce built-in function like this python Syntax (Toggle Plain Text)
number = reduce(lambda x, y: x + y, num)
Try this:
Python Syntax (Toggle Plain Text)
num = [1,2,3,4,5,6] print sum(num)
I upped my sanitary measures, up yours!
Here's a comparison:
Reduce is a way to perform a function cumulatively on every element of a list. It can perform any function, so if you define your own modulus function, it will repeatedly perform that function on each element of the list. In order to avoid defining an entire function for performing x+y, you can instead use a lambda function; which would benefit you more if you googled it, because I'm terrible at explaining them.
If you open up a python interpreter and type help(reduce), this is what you get:
HTH
python Syntax (Toggle Plain Text)
>>> a = [1,2,3,4,5,6] >>> reduce(lambda y,x: x+y, a) 21 >>> sum = 0 >>> for i in a: ... sum+=i ... >>> sum 21 >>>
If you open up a python interpreter and type help(reduce), this is what you get:
python Syntax (Toggle Plain Text)
>>> help(reduce) Help on built-in function reduce in module __builtin__: reduce(...) reduce(function, sequence[, initial]) -> value Apply a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). If initial is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. >>>
Here http://docs.python.org/lib/built-in-funcs.html#l2h-60 is the explanation ! Now, try to understand this example
However,
python Syntax (Toggle Plain Text)
def foo(x, y): return 10 * x + y num = range(1, 7) print num print reduce(foo, num)
sum is nice too, for your specific problem. •
•
Join Date: Dec 2006
Posts: 1,037
Reputation:
Solved Threads: 293
Is anyone else getting a little tired of the "I Googled it and couldn't find anything" lie. "python sum list" brings up "total = sum(list_name)" as the second hit. Searching for "python adding list" gives both the sum() and a for() loop as examples on the fourth hit. But is it a complete waste of time to call them on it? Perhaps we should start posting the Google link. I am more than willing to help anyone with a real programming problem, but this wasting everyone's time can lead to fewer programmers who help because of all of the cruft that one has to wade through.
•
•
•
•
Is anyone else getting a little tired of the "I Googled it and couldn't find anything" lie. "python sum list" brings up "total = sum(list_name)" as the second hit. Searching for "python adding list" gives both the sum() and a for() loop as examples on the fourth hit. But is it a complete waste of time to call them on it? Perhaps we should start posting the Google link. I am more than willing to help anyone with a real programming problem, but this wasting everyone's time can lead to fewer programmers who help because of all of the cruft that one has to wade through.
I wish I could just delete these posts and stop wasting our collective time.
•
•
Join Date: Sep 2008
Posts: 33
Reputation:
Solved Threads: 0
•
•
•
•
Is anyone else getting a little tired of the "I Googled it and couldn't find anything" lie. "python sum list" brings up "total = sum(list_name)" as the second hit. Searching for "python adding list" gives both the sum() and a for() loop as examples on the fourth hit. But is it a complete waste of time to call them on it? Perhaps we should start posting the Google link. I am more than willing to help anyone with a real programming problem, but this wasting everyone's time can lead to fewer programmers who help because of all of the cruft that one has to wade through.

Don't tell me I lied. You don't know if I really googled it or not. The fact is, there is a reason this forum exists, and it is to help people struggling with python. If I can't post here without being flamed, then I will gladly go somewhere else.
It is a sad day when a person can't look to his peers to learn something new in life. If you don't want to answer my post, then don't. But you just wasted your time posting your reply to insult me.
•
•
Join Date: Sep 2008
Posts: 33
Reputation:
Solved Threads: 0
Oh, and one more thing. Woooee, I don't know what google you are using, but for me googling 'python adding list' did not give me the same results that you apparently got. In fact, when I was googling, the information that I came across the most was how to add items to a list. Besides, you knew what you were looking for. I had to wade through pages of documentation, trying to decode what it was saying, trying to determine if it was the answer to my question or not.
![]() |
Similar Threads
- Adding To a list box from multiple text boxes (Visual Basic 4 / 5 / 6)
- Linked List Problem (C++)
- error C2447: '{' : missing function header (old-style formal list?) (C++)
- adding & subtracting objects from a list (C++)
- Adding Listbox Items (Pascal and Delphi)
- adding to list? (VB.NET)
- Populating & Retrieving Data in a listbox : ASP.NET (w/ VB.NET) (ASP.NET)
Other Threads in the Python Forum
- Previous Thread: class in python
- Next Thread: What is Jargon?
| Thread Tools | Search this Thread |
address anydbm app beginner changecolor cipher class conversion coordinates corners curves definedlines development dictionary dynamic events examples excel feet file float font format ftp function generator getvalue gui handling homework images import input ip java keycontrol line linux list lists loan loop maintain maze millimeter mouse mysqldb number numbers output parsing path permissions port prime programming projects py2exe pygame pymailer python queue random rational raw_input recursion recursive scrolledtext searchingfile shebang slicenotation split string strings table tails terminal text thread threading time tkinter tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 variable variables vigenere web windows wx.wizard wxpython xlwt






