sum()

Total up a sequence of numbers — and only numbers; strings are refused on purpose.

Built-in functionPython 2.3+Live demo
Common call
sum(prices)
Returns
int or float; empty input → start (0)
Replaces
sum of an empty list is 0, never an error
Watch out
strings are rejected — join them instead
sum(iterableiterableNumbers to add. Any iterable: list, tuple, generator, range.type: iterable[number] · required, startstartAdded to the total; also the result for an empty iterable.type: number · default: 0=0)
int | float

Demo

Live evaluation
Try:
Inputs
itemslistcomma-separated numbers
startintadded to total
Output
sum([1, 2, 3, 4])
10

Adds left to right, starting from start. The floats case shows binary float reality: 0.1 + 0.2 is 0.30000000000000004 — exactly what Python prints. An empty iterable is not an error; it returns start.

Parameters

NameTypeRequiredDescription
iterableiterable[number]yesNumbers to add. Any iterable: list, tuple, generator, range.
startnumberno (0)Added to the total; also the result for an empty iterable.

Return value

int | floatstart plus every item, left to right. Empty iterable returns start (0 by default).

Common patterns

Sum of transformed values
A generator expression feeds sum without building a list.
total = sum(item.price for item in cart)
Counting matches
True is 1 — summing booleans counts them.
n_even = sum(x % 2 == 0 for x in nums)
Precise money totals
Floats drift; Decimal or integer cents do not.
total_cents = sum(item.cents for item in cart)

Examples

1. Sum a list
sum([1, 2, 3, 4])
Returns
10
2. Float drift
sum([0.1, 0.2])
Returns
0.30000000000000004
3. With a start value
sum([1, 2, 3], 10)
Returns
16
4. Empty is zero
sum([])
Returns
0

Pitfalls

1. Strings are rejected on purpose
Python blocks sum for strings because repeated + is quadratic — the error even names the fix.
Raises
sum(["a", "b"], "")
TypeError: sum() can't sum strings [use ''.join(seq) instead]
Fix
"".join(["a", "b"])
'ab'
2. Float accumulation drifts
Binary floats cannot represent most decimals; errors accumulate.
Drift
sum([0.1] * 10)
0.9999999999999999
Precise
import math
math.fsum([0.1] * 10)
1.0
3. Summing lists with start=[] is quadratic
It works, but copies grow on every step — flatten differently.
Slow
flat = sum(lists, [])
O(n²) copying
Fix
from itertools import chain
flat = list(chain.from_iterable(lists))
O(n)

When to use

Use it
  • Totals of numeric data
  • Counting matches via boolean sums
  • Generator-fed aggregation without temp lists
Reach for something else
  • Strings → str.join
  • Precise float totals → math.fsum
  • Flattening lists → itertools.chain
  • Products → math.prod

Notes

Complexity
O(n)
Return
int stays int; any float makes it float
CPython impl
Python/bltinmodule.c :: builtin_sum (fast paths for int/float)
Memory
No allocation with a generator input
Thread-safe
Yes for the scan; the source should not mutate concurrently

FAQ

Repeated string + is O(n²); join is linear. The TypeError message literally tells you to use join — a deliberate guard rail.

History

2.3
sum() introduced, with the string rejection in place from day one.