Suppose I have a list of integers like [2, 4, 7, 12, 3]
. How can I add all of the numbers together, to get 28
?
Asked
Active
Viewed 1.2e+01k times
10

Karl Knechtel
- 62,466
- 11
- 102
- 153

MaxwellBrahms
- 199
- 1
- 1
- 5
-
1Imagine that someone was going to read these numbers to you one at a time, and you had a piece of paper and a pencil and when the other person got to the end of the list you had to have the sum of all the numbers. What would you do to do this? Now take that concept, and turn it into a Python program. – PaulMcG Dec 17 '12 at 06:23
-
10Note, If you Google the topic this is one of the first questions. Just because you believe something is easy enough to research yourself doesnt mean it doesnt belong here. – Josh Hunt Mar 05 '14 at 13:23
-
Voting to re-open: the question is well posed; searches on the topic often don't find Stack Overflow questions even though it's easily found introductory material; the existing "canonical" is terrible because it confuses two completely separate questions (even after extensive editing to make any sense at all); the original answers cover the bases properly; I can't find anything better after extensive efforts. – Karl Knechtel Sep 09 '22 at 05:35
-
Related: https://stackoverflow.com/questions/16632124 – Karl Knechtel Mar 07 '23 at 22:01
4 Answers
23
x = [2, 4, 7, 12, 3]
sum_of_all_numbers= sum(x)
or you can try this:
x = [2, 4, 7, 12, 3]
sum_of_all_numbers= reduce(lambda q,p: p+q, x)
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 p+q, you can instead use a lambda function.

The Recruit
- 803
- 3
- 9
- 18
-
When I did this I got a NameError, it said global name 'reduce' is not defined – MaxwellBrahms Dec 17 '12 at 06:21
-
Works great for me.>>> x = [2, 4, 7, 12, 3] >>> sum_of_all_numbers= reduce(lambda q,p: p+q, x) >>> print sum_of_all_numbers 28 >>> – The Recruit Dec 17 '12 at 06:32
-
1The only reason i can decipher is probably You are using Python 3, and you are following a tutorial designed for Python 2.x.. reduce has been removed from built in tools of python 3.. Still if you want to use reduce you can, by importing it from functools module. Jus add a stmt at the top as import functools happy coding :) let me now if it works for u. – The Recruit Dec 17 '12 at 06:48
-
If you are content with the solution you could probably upvote the answer so that it doesnt get bogged down as time passes :) Happy Coding :) – The Recruit Dec 17 '12 at 07:30
-
3In what sense is `reduce(lambda q,p: p+q, x)` "more efficient" than `sum(x)`? – Eric Dec 17 '12 at 10:10
-
4`reduce` is **not** more efficient than `sum` for numeric types, Otherwise `sum` would never have been added to the language. Also it is more efficient to use `operator.add` in place of the lambda function – John La Rooy Dec 17 '12 at 22:05
15
This:
sum([2, 4, 7, 12, 3])
You use sum()
to add all the elements in a list.
So also:
x = [2, 4, 7, 12, 3]
sum(x)

jackcogdill
- 4,900
- 3
- 30
- 48
-
-
Well, you can do it manually in a loop or use `reduce()` as The Recruit suggested. – jackcogdill Dec 17 '12 at 06:14
4
First Way:
my_list = [1,2,3,4,5]
list_sum = sum(list)
Second Way(less efficient):
my_list = [1,2,3,4,5]
list_sum = 0
for x in my_list:
list_sum += x

Community
- 1
- 1

RandomPhobia
- 4,698
- 7
- 25
- 22
-
Your first example won't work. You need to change the 2nd line to `list_sum = sum(my_list)` – MacItaly Jun 12 '19 at 19:57
3
you can try :
x = [2, 4, 7, 12, 3]
total = sum(x)

Yuanhang Guo
- 466
- 4
- 9
-
The built-in functions in python: http://docs.python.org/2/library/functions.html#sum – Yuanhang Guo Dec 17 '12 at 06:03