How to sum the items of an iterable in Python

The sum() function allows you to sum all of the items of an iterable in Python.

Dictionaries, lists, and tuples are examples of iterables.

This example sums the numbers 4, 8, 16, 32, 64 of a list.

my_list = [4, 8, 16, 32, 64]

print(sum(my_list))
124

You can also specify an extra value to add, simply pass the extra number as the second argument of the function.

my_list = [4, 8, 16, 32, 64]

print(sum(my_list, 32))
156