Skip to content

Effortlessly Calculate List Sum in Python


Python’s sum(): The Pythonic Way to Sum Values

Python’s built-in function sum() is an efficient and Pythonic way to sum a list of numeric values. Adding several numbers together is a common intermediate step in many computations, so sum() is a pretty handy tool for a Python programmer.

As an additional and interesting use case, you can concatenate lists and tuples using sum(), which can be convenient when you need to flatten a list of lists.

In this tutorial, you’ll learn how to:

  • Sum numeric values by hand using general techniques and tools
  • Use Python’s sum() to add several numeric values efficiently
  • Concatenate lists and tuples with sum()
  • Use sum() to approach common summation problems
  • Use appropriate values for the arguments in sum()
  • Decide between sum() and alternative tools to sum and concatenate objects

This knowledge will help you efficiently approach and solve summation problems in your code using either sum() or other alternative and specialized tools.

Understanding the Summation Problem

Summing numeric values together is a fairly common problem in programming. For example, say you have a list of numbers [1, 2, 3, 4, 5] and want to add them together to compute their total sum. With standard arithmetic, you’ll do something like this:

1 + 2 + 3 + 4 + 5 = 15

As far as math goes, this expression is pretty straightforward. It walks you through a short series of additions until you find the sum of all the numbers.

It’s possible to do this particular calculation by hand, but imagine some other situations where it might not be so possible. If you have a particularly long list of numbers, adding by hand can be inefficient and error-prone. What happens if you don’t even know how many items are in the list? Finally, imagine a scenario where the number of items you need to add changes dynamically or unpredictably.

In situations like these, whether you have a long or short list of numbers, Python can be quite useful to solve summation problems.

If you want to sum the numbers by creating your own solution from scratch, then you can try using a for loop:

numbers = [1, 2, 3, 4, 5]
total = 0
for number in numbers:
total += number

Here, you first create total and initialize it to 0. Then, you iterate over each number in the list numbers using a for loop. In each iteration, you add the current number to the total. Finally, you get the result of total, which will be the sum of all the numbers in the list.

While this approach works fine, Python provides a built-in function sum() that can accomplish this same task in a much more concise and efficient way.

Getting Started With Python’s sum()

Python’s sum() function takes an iterable as its required argument and returns the sum of all the elements in the iterable. An iterable can be any object that can be looped over, such as a list, tuple, string, or range.

The Required Argument: iterable

The required argument for sum() is the iterable that contains the items you want to sum. Let’s take a look at some examples:

numbers = [1, 2, 3, 4, 5]
total = sum(numbers)

In this example, the iterable numbers contains the numbers you want to sum. By passing numbers as the argument to sum(), you get the result of the sum directly.

The Optional Argument: start

The sum() function also accepts an optional argument start, which is set to 0 by default. This argument specifies the initial value for the sum.

numbers = [1, 2, 3, 4, 5]
total = sum(numbers, 10)

In this example, instead of starting the sum from 0, you specify 10 as the initial value for the sum. The sum() function then adds all the elements in numbers to 10 and returns the result.

Summing Numeric Values

Now that you understand the basics of using sum(), let’s explore some common use cases for summing numeric values.

Concatenating Sequences

One interesting property of the sum() function in Python is that it can be used to concatenate sequences, such as lists or tuples.

list1 = [1, 2, 3]
list2 = [4, 5, 6]
concatenated_list = sum([list1, list2], [])

In this example, you have two lists list1 and list2. By passing [list1, list2] as the argument to sum(), you concatenate the two lists into a single list. The second argument [] is provided as an empty list, which acts as the initial value for the sum. The result is a flattened list containing all the elements from list1 and list2.

Practicing With Python’s sum()

In addition to summing numeric values, sum() can be used to solve various other summation problems. Let’s explore a few examples:

Computing Cumulative Sums

numbers = [1, 2, 3, 4, 5]
cumulative_sums = [sum(numbers[:i+1]) for i in range(len(numbers))]

In this example, you have a list of numbers numbers. Using list comprehension, you calculate the cumulative sum up to each element in numbers. The result is a new list cumulative_sums that contains the cumulative sums.

Calculating the Mean of a Sample

numbers = [1, 2, 3, 4, 5]
mean = sum(numbers) / len(numbers)

Here, you calculate the mean of a sample by summing the numbers in the list numbers and dividing it by the length of the list. The result is the average value of the numbers in the list.

Finding the Dot Product of Two Sequences

sequence1 = [1, 2, 3]
sequence2 = [4, 5, 6]
dot_product = sum(number1 * number2 for number1, number2 in zip(sequence1, sequence2))

In this example, you have two sequences sequence1 and sequence2. Using the zip() function, you iterate over the corresponding elements of both sequences and multiply them together. The sum() function then calculates the dot product by summing all the resulting products.

Flattening a List of Lists

list_of_lists = [[1, 2], [3, 4, 5], [6, 7, 8, 9]]
flattened_list = sum(list_of_lists, [])

Here, you have a list of lists list_of_lists. By passing list_of_lists as the argument to sum(), you concatenate all the inner lists into a single flattened list.

Using Alternatives to sum()

While Python’s sum() function is quite powerful and convenient, there are alternative tools that you can use for specific summation tasks. Let’s explore a few of them:

Summing Floating-Point Numbers: math.fsum()

If you need to sum floating-point numbers with high precision, you can use the math.fsum() function from the math module.

import math
numbers = [0.1, 0.1, 0.1, 0.1, 0.1]
total = math.fsum(numbers)

In this example, you have a list of floating-point numbers numbers. By using math.fsum() instead of sum(), you get a more accurate result due to improved precision handling.

Concatenating Iterables With itertools.chain()

The itertools.chain() function can be used to concatenate any number of iterables. It takes multiple iterables as arguments and returns a single iterator that produces all the elements from each iterable in the order they appear.

from itertools import chain
list1 = [1, 2, 3]
list2 = [4, 5, 6]
concatenated_list = list(chain(list1, list2))

In this example, you use itertools.chain() to concatenate list1 and list2 into a single list. The result is a new list concatenated_list that contains all the elements from both lists.

Concatenating Strings With str.join()

If you want to concatenate strings element-wise, you can use the str.join() method.

words = ['Hello', 'World']
combined_string = ''.join(words)

In this example, you have a list of strings words. Using the str.join() method with an empty string '', you concatenate all the strings in the list into a single string.


Python’s sum() function is a versatile tool that allows you to efficiently sum numeric values and concatenate sequences. By mastering its usage and understanding alternative tools, you can effectively solve various summation problems in your code. Whether you’re adding numbers, flattening lists, or performing other summation tasks, sum() provides a Pythonic way to get the job done.

Now that you have a better understanding of Python’s sum() function and its capabilities, you can start using it in your own code to simplify and optimize your computations. Happy coding!