adding list items

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2008
Posts: 33
Reputation: jworld2 is an unknown quantity at this point 
Solved Threads: 0
jworld2 jworld2 is offline Offline
Light Poster

adding list items

 
0
  #1
Sep 28th, 2008
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?
Impossible Physics
Graphics and Imaging
http://www.impossiblephysics.com
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 953
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 218
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: adding list items

 
0
  #2
Sep 28th, 2008
Use the reduce built-in function like this
  1. number = reduce(lambda x, y: x + y, num)
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 33
Reputation: jworld2 is an unknown quantity at this point 
Solved Threads: 0
jworld2 jworld2 is offline Offline
Light Poster

Re: adding list items

 
0
  #3
Sep 28th, 2008
Could you tell me what this means?
Impossible Physics
Graphics and Imaging
http://www.impossiblephysics.com
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,540
Reputation: Lardmeister is an unknown quantity at this point 
Solved Threads: 22
Lardmeister's Avatar
Lardmeister Lardmeister is offline Offline
Posting Virtuoso

Re: adding list items

 
0
  #4
Sep 28th, 2008
Try this:
  1. num = [1,2,3,4,5,6]
  2. print sum(num)
I upped my sanitary measures, up yours!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,057
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 266
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: adding list items

 
0
  #5
Sep 28th, 2008
Here's a comparison:
  1. >>> a = [1,2,3,4,5,6]
  2. >>> reduce(lambda y,x: x+y, a)
  3. 21
  4. >>> sum = 0
  5. >>> for i in a:
  6. ... sum+=i
  7. ...
  8. >>> sum
  9. 21
  10. >>>
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:
  1. >>> help(reduce)
  2. Help on built-in function reduce in module __builtin__:
  3.  
  4. reduce(...)
  5. reduce(function, sequence[, initial]) -> value
  6.  
  7. Apply a function of two arguments cumulatively to the items of a sequence,
  8. from left to right, so as to reduce the sequence to a single value.
  9. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
  10. ((((1+2)+3)+4)+5). If initial is present, it is placed before the items
  11. of the sequence in the calculation, and serves as a default when the
  12. sequence is empty.
  13.  
  14. >>>
HTH
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 953
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 218
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: adding list items

 
0
  #6
Sep 28th, 2008
Here http://docs.python.org/lib/built-in-funcs.html#l2h-60 is the explanation ! Now, try to understand this example
  1. def foo(x, y):
  2. return 10 * x + y
  3.  
  4. num = range(1, 7)
  5. print num
  6. print reduce(foo, num)
However, sum is nice too, for your specific problem.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,037
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 293
woooee woooee is offline Offline
Veteran Poster

Re: adding list items

 
0
  #7
Sep 28th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,057
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 266
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: adding list items

 
0
  #8
Sep 28th, 2008
Originally Posted by woooee View Post
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.
It is increasingly rare to find anything else on this forum. I left a forum before coming here solely based on this fact. Endless "I couldn't find the answer anywhere else..." and "I have a problem: I'm trying to make a program that '<copy_and_paste from Instructions for homework' and I'm stuck, don't have any code, so how would I go about this? And it's super urgent PLS help"

I wish I could just delete these posts and stop wasting our collective time.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 33
Reputation: jworld2 is an unknown quantity at this point 
Solved Threads: 0
jworld2 jworld2 is offline Offline
Light Poster

Re: adding list items

 
0
  #9
Sep 28th, 2008
Originally Posted by woooee View Post
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.
Now that hurts.

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.
Impossible Physics
Graphics and Imaging
http://www.impossiblephysics.com
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 33
Reputation: jworld2 is an unknown quantity at this point 
Solved Threads: 0
jworld2 jworld2 is offline Offline
Light Poster

Re: adding list items

 
0
  #10
Sep 28th, 2008
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.
Impossible Physics
Graphics and Imaging
http://www.impossiblephysics.com
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC