We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,939 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

adding list items

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?

5
Contributors
9
Replies
5 Hours
Discussion Span
4 Years Ago
Last Updated
11
Views
Question
Answered
jworld2
Light Poster
35 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Use the reduce built-in function like this

number = reduce(lambda x, y: x + y, num)
Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

Could you tell me what this means?

jworld2
Light Poster
35 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Try this:

num = [1,2,3,4,5,6]
print sum(num)
Lardmeister
Posting Virtuoso
1,938 posts since Mar 2007
Reputation Points: 465
Solved Threads: 72
Skill Endorsements: 5

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

jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 293
Skill Endorsements: 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.

Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

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.

woooee
Posting Maven
2,705 posts since Dec 2006
Reputation Points: 827
Solved Threads: 779
Skill Endorsements: 9

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.

jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 293
Skill Endorsements: 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.

jworld2
Light Poster
35 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

jworld2
Light Poster
35 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 4 Years Ago by jlm699, Gribouillis, woooee and 1 other

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0854 seconds using 2.68MB