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?

Use the reduce built-in function like this

number = reduce(lambda x, y: x + y, num)

Could you tell me what this means?

Try this:

num = [1,2,3,4,5,6]
print sum(num)

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

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.

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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.