Python How To Sum A List
We are going to show you how to sum a list using Python. This can be done using the sum() function. You don’t need to bother looping and using counter variables or anything like that.
To sum a list in Python just do the following:
a = [47, 23, 4, 55, 233, 9] result = sum(a) print(result)
You could also reduce this to a single line like this:
print( sum([47, 23, 4, 55, 233, 9]))
Sum a List in Python The “Hard” Way
Don’t’ do it the “hard” way. It is easy enough but you would basically be reinventing the wheel. Just use the builtin sum() function we covered above.
a = [47, 23, 4, 55, 233, 9] total = 0 for i in a: total += i print(total)