The sum() function is an incredibly useful built-in function in Python. It allows you to easily sum the values in an iterable such as a list, tuple, or set.
In this comprehensive guide, you‘ll learn all about how to use Python‘s sum() function with clear examples.
Overview of the Sum Function
The sum() function takes an iterable like a list as the argument and returns the sum of all the elements in that iterable.
For example:
nums = [1, 2, 3, 4, 5]
result = sum(nums)
print(result) # 15
This makes it very easy to get the total sum of numbers in an iterable without needing to loop through and add them up manually.
The syntax for sum() is:
sum(iterable, start=0)
iterable– A required iterable like a list, tuple, set containing numeric elements to be summed up.start– An optional starting value that the sum gets added to. Defaults to 0.
Let‘s first look at how to use sum() with some common Python data types like lists, tuples, and sets.
Using Sum() With Lists
Lists are one of the most common uses for sum().
Here‘s a simple example:
nums = [1, 2, 3, 4, 5]
total = sum(nums)
print(total) # 15
This sums up all the numbers in the nums list and returns the total sum.
You can also initialize a start value. Here‘s how to add 100 to the sum:
nums = [1, 2, 3, 4, 5]
total = sum(nums, 100)
print(total) # 115
The start value 100 gets added to the final sum.
This works great when you need to calculate a running sum and want to add previous values to a new sum.
Using Sum() With Tuples
The sum() function can sum up values in a tuple as well.
For example:
nums = (1, 2, 3, 4, 5)
total = sum(nums)
print(total) # 15
Tuples work identically to lists. The only difference is that tuples are immutable.
Using Sum() With Sets
sum() can also be used with Python sets which contain unique elements.
For example:
nums = {1, 2, 3, 4, 5}
total = sum(nums)
print(total) # 15
One difference with sets is that the order may be random since sets are unordered. But the sum is still calculated correctly.
Using Sum() With Dictionaries
Dictionaries are another very common Python data type.
However, sum() works a bit differently with dictionaries than other iterables.
By default, sum() will sum up the keys in a dictionary rather than the values.
For example:
nums = {‘one‘: 1, ‘two‘: 2, ‘three‘: 3}
total = sum(nums)
print(total) # 6
This sums up the keys ‘one‘, ‘two‘, and ‘three‘ rather than the values 1, 2, and 3.
To sum the values, you need to explicitly convert the dictionary to a list of values first using the values() method:
nums = {‘one‘: 1, ‘two‘: 2, ‘three‘: 3}
total = sum(nums.values())
print(total) # 6
Now sum() will correctly total up the values in the dictionary.
Using Sum() on Nested Lists
One very useful feature of sum() is that it can flatten nested iterables like lists.
For example:
nested_list = [[1, 2], [3, 4], [5, 6]]
total = sum(nested_list, [])
print(total) # [1, 2, 3, 4, 5, 6]
By passing an empty list as the start value, sum() will flatten out the nested list and return a one-dimensional list with all the values.
This makes it very easy to flatten irregular nested lists into a single flat list.
Using Sum() with Numpy Arrays
The sum() function also works nicely with Numpy arrays:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
total = sum(arr)
print(total) # 15
This provides a simple way to get the sum of all elements across any axes in a Numpy array.
Summing Over a Specific Axis in a Multidimensional Numpy Array
When working with multidimensional Numpy arrays, you can sum over a specific axis using sum() and pass in the axis parameter.
For example:
arr = np.array([[1, 2], [3, 4]])
col_sum = sum(arr, axis=0)
print(col_sum) # [4 6]
row_sum = sum(arr, axis=1)
print(row_sum) # [3 7]
Here axis=0 sums over the columns while axis=1 sums over the rows.
This provides a very flexible way to sum over dimensions in Numpy arrays.
Summing Multiple Iterables Simultaneously
You can pass multiple iterables to sum() and it will sum them together element-wise.
For example:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
total = sum([list1, list2])
print(total) # [5, 7, 9]
sum() adds the first elements in each list, then the second elements, and so on.
This makes it easy to sum corresponding elements across multiple sequences.
Using Sum() with Generator Expressions
You can use sum() with generator expressions to efficiently generate sums without creating full lists.
For example, to get the sum of squares:
sum(x*x for x in range(10)) # 285
This sums up the square of each number from 0 to 9 without needing to allocate a full list in memory.
Using Sum() with Mixed Data Types
By default, sum() expects all the elements to be numbers. But it will try to coerce elements to numbers automatically.
For example:
mixed = [1, 2, ‘3‘, [4], 5]
total = sum(mixed)
print(total) # 15
Here the string ‘3‘ and list [4] are coerced to numbers 3 and 4 respectively.
However, this can lead to unexpected results in some cases. So it‘s best to ensure the iterable contains strictly numeric data.
Difference Between Sum() and Summation (\Sigma) Notation
Python‘s sum() function is related to but different from summation (\Sigma) notation in mathematics.
Summation notation indicates summing a sequence of numbers, for example:
Σ(i=1 to n) i
Which means sum all integers from 1 to n.
The key differences are:
sum()takes any iterable, not just an integer range.sum()sums the elements themselves, not their indices.sum()works on any numeric data type, not just integers.
So sum() is a generalization of summation notation to work broadly across Python rather than literally matching mathematical notation.
Handling Empty Iterables
If you pass an empty iterable to sum(), it simply returns the start value:
empty_list = []
total = sum(empty_list, 0)
print(total) # 0
So with a start value of 0, it handles empty iterables gracefully.
Without a start value, sum() will raise a TypeError on empty iterables:
empty_list = []
total = sum(empty_list) # TypeError
So it‘s best to provide a start value like 0 when there‘s a possibility the iterable may be empty.
Alternative: Looping Over a List
Manually looping over a list and calculating the sum is a bit more code, but works as an alternative to using sum():
nums = [1, 2, 3, 4, 5]
total = 0
for num in nums:
total += num
print(total) # 15
However, sum() provides a cleaner and more Pythonic approach in most cases.
Alternative: Using Reduce()
You can also sum values using reduce() and a lambda function:
from functools import reduce
nums = [1, 2, 3, 4, 5]
total = reduce(lambda x, y: x + y, nums)
print(total) # 15
But for simple summing, sum() is preferable over reduce().
Pros and Cons of the Sum() Function
Some key advantages of using sum() include:
- Simple and clean syntax.
- Works across many data types like lists, tuples, sets, Numpy arrays.
- Handles nested lists and multidimensional arrays.
- More efficient than manual looping or
reduce().
Some disadvantages:
- Default behavior with dictionaries can cause confusion.
- Auto-coercion of data types can lead to subtle bugs.
- Built-in function so difficult to extend or customize.
So in general sum() is the best approach for summing iterables in Python in most cases.
Conclusion
Python‘s sum() function provides a simple way to get the total sum across iterables like lists, tuples, and arrays.
Key takeaways:
- Accepts any iterable containing numeric elements
- Can specify starting value to add to the sum
- Flattens and sums nested lists by default
- Sums values in dictionaries, not keys, by default
- Works element-wise on multiple iterables
- Handles empty iterables and mixed data types
Learning to use sum() can help you simplify code that performs numeric summation in Python.