Skip to main content
Lists

How to sum a list in Python

Quick answer

Use the built-in sum: total = sum(my_list) - fast, handles empty lists, works with any numeric iterable.

my_list = [1, 2, 3, 4]
total = sum(my_list)
print(total)

The snippet creates a list of numbers, feeds it to Python's built‑in sum function, and prints the resulting total. sum iterates once, runs in C speed, and returns 0 for an empty iterable, so you avoid manual length checks. This approach wins because it is concise, leverages the highly optimized CPython implementation, and works with any iterable of ints, floats, or objects that implement __add__. Gotcha: sum starts with 0, so mixing numeric types (e.g., int and Decimal) can raise TypeError; supply an appropriate start value in those cases.

Variations

Loop with accumulator

total = 0
for n in my_list:
    total += n

Explicit loop, useful when you need side‑effects or custom logic.

Sum with start value for non‑numeric types

from decimal import Decimal
my_list = [Decimal('1.1'), Decimal('2.2')]
total = sum(my_list, Decimal(0))

Provides a neutral start for Decimal or Fraction to avoid TypeError.

Sum values from a dict

prices = {'a': 10, 'b': 20}
total = sum(prices.values())

Same pattern works for any mapping of numeric values.

Practise this in your browser

Every how-to has a live lesson. Free tier, no card.

Start free →

Unlock every lesson

Pro — $12/mo, unlimited AI, cancel any time.

See Pro plans →

Get one Python lesson + one career idea every Friday

No spam, no "buy our course now". Three bullets, every Friday. Unsubscribe with one click.

Related how-tos