1,105,585 Community Members

adding list items

Member Avatar
jworld2
Light Poster
35 posts since Sep 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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?

Member Avatar
Gribouillis
Posting Maven
3,454 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 884 [?]
Skill Endorsements: 18 [?]
Moderator
 
0
 

Use the reduce built-in function like this

number = reduce(lambda x, y: x + y, num)
Member Avatar
jworld2
Light Poster
35 posts since Sep 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Could you tell me what this means?

Member Avatar
Lardmeister
Posting Virtuoso
1,966 posts since Mar 2007
Reputation Points: 434 [?]
Q&As Helped to Solve: 111 [?]
Skill Endorsements: 8 [?]
 
0
 

Try this:

num = [1,2,3,4,5,6]
print sum(num)
Member Avatar
jlm699
Veteran Poster
1,104 posts since Jul 2008
Reputation Points: 320 [?]
Q&As Helped to Solve: 295 [?]
Skill Endorsements: 0 [?]
 
0
 

Here's a comparison:

>>> 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
>>>

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:

>>> 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.

>>>

HTH

Member Avatar
Gribouillis
Posting Maven
3,454 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 884 [?]
Skill Endorsements: 18 [?]
Moderator
 
0
 

Here http://docs.python.org/lib/built-in-funcs.html#l2h-60 is the explanation ! Now, try to understand this example

def foo(x, y):
  return 10 * x + y

num = range(1, 7)
print num
print reduce(foo, num)

However, sum is nice too, for your specific problem.

Member Avatar
woooee
Posting Maven
2,798 posts since Dec 2006
Reputation Points: 783 [?]
Q&As Helped to Solve: 836 [?]
Skill Endorsements: 12 [?]
 
-1
 

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.

Member Avatar
jlm699
Veteran Poster
1,104 posts since Jul 2008
Reputation Points: 320 [?]
Q&As Helped to Solve: 295 [?]
Skill Endorsements: 0 [?]
 
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.

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.

Member Avatar
jworld2
Light Poster
35 posts since Sep 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

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.

Member Avatar
jworld2
Light Poster
35 posts since Sep 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Question Answered as of 5 Years Ago by Gribouillis, jlm699, woooee and 1 other
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article