How to Sum Elements in a List in Python

While working on a data analytics project for a U.S.-based retail client, I needed to calculate the total daily sales from a list of transactions. Each value represented the sales amount for a day, stored neatly in a Python list.

At first, I thought of using the built-in sum() function, but then I realized there are multiple ways to sum elements in a list in Python, some more flexible and efficient depending on the situation.

In this tutorial, I’ll show you four simple and practical methods to sum elements in a list in Python. These methods range from using a for loop, the sum() function, list comprehension, and even the reduce() function.

Sum Elements in a List in Python

Multiple methods exist to sum elements in a list in Python, such as for loop, sum(), list comprehension, and reduce() methods. Let’s see each technique one by one.

Method 1: Use For Loop in Python

The most obvious way to add numbers stored in a list is to use a for-loop. The following code fragment visits each list element in order and totals the sum of its elements.

For example, look at the logic of the code below.

# List of daily sales figures
daily_sales = [250, 450, 300, 400, 500, 600, 350]

# Initialize total_sales to 0
total_sales = 0

# Iterate over each sale in the daily_sales list
for sale in daily_sales:
    # Add the current sale to the running total
    total_sales += sale

# Print the total sales for the week using an f-string
print(f"Total sales for the week: ${total_sales}")

You can refer to the screenshot below to see the output.

Sum Elements in a List in Python Using For Loop

From the output, you can see that all the daily sales in the list are added together.

Method 2: Use sum() Method in Python

A Python built-in function: sum(). Another way to shorten the syntax for summing elements in the list is by using the built-in function.

For example, run the code below to sum the list.

# List of daily sales figures
daily_sales = [250, 450, 300, 400, 500, 600, 350]

# Calculate the total sales by summing up all values in the daily_sales list
total_sales = sum(daily_sales)

# Print the total sales for the week using an f-string
print(f"Total sales for the week: ${total_sales}")

You can refer to the screenshot below to see the output.

Sum Elements in a List in Python Using sum() Method

Method 3: Use Python List Comprehension

A list comprehension is a more concise way to create and combine lists with the sum() function in more complex cases.

For example, run the code below to understand how to sum list elements using list comprehension, where you need to calculate the sales to be greater than 300 dollars.

# List of daily sales figures
daily_sales = [250, 450, 300, 400, 500, 600, 350]

# Calculate the total sales for days with sales over $300
# using a list comprehension and the sum() function
total_sales = sum([sale for sale in daily_sales if sale > 300])

# Print the total sales for days with sales over $300 using an f-string
print(f"Total sales for days with sales over $300: ${total_sales}")

You can refer to the screenshot below to see the output.

Sum Elements in a List in Python Using List Comprehension

Method 4: Use Python’s reduce() Method

The general form of reduce() from the functools module is a rolling computation on successive pairs of elements from a sequence. This style doesn’t occur often, but it can sometimes be quite helpful.

Let’s see with an example.

# Import the reduce function from the functools module
from functools import reduce

# List of daily expenditures
expenditures = [1200, 1500, 1100, 1400, 1700, 1600, 1300]

# Calculate the total expenditure for the month using reduce and a lambda function
total_expenditure = reduce(lambda x, y: x + y, expenditures)

# Print the total expenditure for the month using an f-string
print(f"Total expenditure for the month: ${total_expenditure}")

You can refer to the screenshot below to see the output.

Sum Elements in a List in Python Using reduce() Method

While there are multiple ways to sum elements in a list in Python, the best method depends on your use case.

If you’re learning or working with small datasets, the for loop or built-in sum() function is perfect. For large-scale data or performance-critical tasks, the reduce() function is the way to go.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.