How do I calculate the sum of the elements of a Python list?

To calculate the sum of the elements of a Python list, you can use the built-in sum(iterable, start) method. The sum() method takes an iteration as its first argument and an optional starting number as the second argument sums the items and returns the result. The optional "start" argument adds the specified value to the result (defaults to 0). If the elements are not numbers, a TypeError exception will be thrown. In this Python example, we calculate the sum of the list elements using the sum() method and add an initial value to the result, passing it as the second argument. Click Execute to run Python List Sum Example online and see the result.
How do I calculate the sum of the elements of a Python list? Execute
my_list = [1, 2, 3, 4, 5]

print(sum(my_list, 100))
Updated: Viewed: 1133 times

Python List Sum Syntax

Following is the syntax of the sum() method:

Python sum() Example
sum(iterable, start)

Where:
  • iterable: the list where the elements of the iteration must be numbers
  • start (optional): the initial value to add to the result. If not specified, the default is 0.

See also