How to Use The Python sum() Function

I keep coming back to the Python sum() function whenever I need a quick way to add up numbers. It is one of those built-in tools that seems simple on the surface but has enough quirks to trip you up if you skip the fine print.

This article covers the Python sum() function from the ground up. By the end, you will know exactly how it handles different iterables, what the optional start parameter does, and when to reach for NumPy instead.

TLDR

  • sum() adds up all items in an iterable, starting from 0 by default
  • The second argument sets a custom starting value instead of 0
  • sum() raises TypeError when iterable items are strings without explicit start
  • NumPy’s np.sum() handles multi-dimensional arrays faster and with more options
  • sum() works on lists, tuples, sets, and even complex numbers

What is sum() in Python?

sum() is a built-in Python function that takes an iterable and returns the total of all its elements. It lives in the standard library, so no imports are needed for basic use. Under the hood, it walks through the iterable and accumulates each item into a running total, then returns that total when it reaches the end.

The syntax is:

sum(iterable, start=0)

The iterable is any object that returns its items one at a time — lists, tuples, sets, and range objects all qualify. The start parameter is the initial value added to the total before any iterable items are included. If you omit start, Python uses 0 as the default.

Basic Examples

Here is how sum() behaves with lists, tuples, sets, and dictionaries.

numbers = [10, 20, 30, 40]
print(sum(numbers))

prices = (12.5, 8.75, 3.0)
print(sum(prices))

unique = {100, 200, 300}
print(sum(unique))

sales = {'a': 10, 'b': 20, 'c': 30}
print(sum(sales))

100
24.25
600
60

The dictionary example deserves a closer look. When you pass a dictionary to sum(), Python iterates over the keys, not the values. That is why sum(sales) returns 60. If you need to sum the values, pass sales.values() instead.

The start Parameter

The second argument to sum() lets you shift the starting point. Instead of beginning at 0, you can add any value first, and then add all iterable elements on top of it.

scores = [85, 90, 78, 92]

total = sum(scores)
print(total)

total_with_bonus = sum(scores, 100)
print(total_with_bonus)

345
445

Using start avoids creating an intermediate object in memory, which matters when you are working with large sequences.

Complex Numbers, Nested Lists, and Common Mistakes

sum() handles complex numbers, and it works with nested lists if you flatten them first.

complex_nums = [4 + 3j, 7 + 5j, 2 + 1j]
print(sum(complex_nums))

nested = [[1, 2], [3, 4], [5, 6]]
flat = sum(nested, [])
print(flat)

(13+9j)
[1, 2, 3, 4, 5, 6]

Flattening with sum() works but creates a new list on every addition. For large nested structures, itertools.chain() or a list comprehension is faster.

Dictionaries also work with sum() — it iterates over keys by default. Passing an empty iterable returns the start value. This is useful when working with dynamic data that might be empty.

empty = []
print(sum(empty))
print(sum(empty, 42))

0
42

The most frequent error is mixing types. If you try to sum a list of strings without providing a start value, Python raises a TypeError because it does not know how to add strings to integers.

words = ['apple', 'banana', 'cherry']

# TypeError without start
# total = sum(words)

# Correct: join with empty string as start
total = sum(words, '')
print(total)

applebananacherry

Python sum() vs NumPy sum()

NumPy has its own sum() function that behaves differently for multi-dimensional arrays. NumPy operations are vectorized, which means they run in C rather than Python bytecode. For large arrays, this difference is significant.

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])

print(np.sum(arr))
print(np.sum(arr, axis=0))
print(np.sum(arr, axis=1))

21
[5 7 9]
[ 6 15]

The built-in sum() cannot handle NumPy arrays row-wise or column-wise. If you are doing numerical work with matrices, NumPy is the right tool. For simple one-dimensional totals, the built-in sum() is lighter and requires no dependency.

FAQ

Q: What does sum() return for an empty list?

sum([]) returns 0, which is the default start value. If you pass a custom start value, that is what gets returned instead.

Q: Can sum() handle strings?

Only with an explicit start value. sum([‘a’, ‘b’], ”) returns ‘ab’. Without a start value, a TypeError is raised because strings cannot be added to integers.

Q: Does sum() work with dictionaries?

sum() iterates over dictionary keys by default. To sum the values, pass the dictionary view object explicitly: sum(d.values()).

Q: Which is faster, sum() or NumPy sum()?

For Python lists with a few dozen elements, the built-in sum() is fast enough. For NumPy arrays with thousands of elements, np.sum() is significantly faster because it executes compiled C code rather than Python loops.

Q: What is the difference between sum() and reduce()?

sum() is a specialized tool for addition. reduce() applies a two-argument callable cumulatively, which means it can handle any operation — multiplication, concatenation, max, and more. sum() is simpler and faster for the specific case of addition.