How to Find the Sum of an Array in Python?

In this tutorial, I will explain how to find the sum of an array in Python. As a data scientist working on a project for a US-based company, I encountered a scenario where I needed to calculate the total sum of values stored in an array. I will explain various methods to achieve this, along with examples and screenshots of executed example code.

Create an Array in Python

Python array is a collection of elements of the same data type. Arrays are commonly used to store and manipulate numerical data. Python provides various ways to create and work with arrays.

Let’s say we have a list of sales figures for a company based in New York. We can create an array to store these values:

sales_figures = [5000, 7500, 8000, 6500, 9000]

Check out How to Remove Duplicates from an Array in Python

Method 1: Use the sum() Function

Python provides a built-in sum() function to calculate the sum of all elements in an array. It is the most simple and efficient way to find the sum of an array. The sum() function in Python takes an iterable (such as a list or array) as an argument and returns the sum of its elements.

Example

Let’s calculate the total sales for the New York-based company using the sum() function:

sales_figures = [5000, 7500, 8000, 6500, 9000]
total_sales = sum(sales_figures)
print("Total sales:", total_sales)

Output:

Total sales: 36000

I have executed the example code and added the screenshot below.

Find the Sum of an Array in Python

Read How to Check if an Array Index Exists in Python

Method 2: Use a For Loop

Another approach to find the sum of an array is by using a for loop to iterate over each element and store their values. This method is used mainly when you need to perform additional operations or apply conditions while calculating the sum.

Example

Let’s calculate the total sales for the company, but this time we’ll use a for loop and only include sales figures above $6000:

sales_figures = [5000, 7500, 8000, 6500, 9000]
total_sales = 0

for sale in sales_figures:
    if sale > 6000:
        total_sales += sale

print("Total sales above $6000:", total_sales)

Output:

Total sales above $6000: 31000

I have executed the example code and added the screenshot below.

How to Find the Sum of an Array in Python

Check out How to Remove NaN from Array in Python

Method 3: Use List Comprehension

List comprehension is a simple way to create list based on existing lists. It allows you to combine the functionality of a for loop and conditional statements in a single line of code. We can use list comprehension to calculate the sum of an array by generating a new list with the desired elements and then applying the sum() function.

Example

Let’s calculate the total sales for the company using list comprehension, considering only sales figures below $8000:

sales_figures = [5000, 7500, 8000, 6500, 9000]
total_sales = sum([sale for sale in sales_figures if sale < 8000])
print("Total sales below $8000:", total_sales)

Output:

Total sales below $8000: 19000
Find the Sum of an Array in Python Using List Comprehension

Read How to Check if an Array Contains a Value in Python

Handle Large Arrays in Python

When working with large arrays, it’s essential to consider the performance and memory usage of your code. Python’s built-in sum() function is optimized for efficiency and is generally the preferred choice for summing large arrays. However, if you need to perform additional operations or apply complex conditions while calculating the sum, using a for loop or list comprehension might be more suitable.

Example

Let’s say we have a large array of sales figures for multiple stores across the United States. We want to calculate the total sales for stores with sales above $10,000.

sales_figures = [12000, 8000, 15000, 9500, 20000, 7000, 18000, 11500, 6000, 14000]
total_sales = sum([sale for sale in sales_figures if sale > 10000])
print("Total sales above $10,000:", total_sales)

Output:

Total sales above $10,000: 90500

Check out Check if NumPy Array is Empty in Python

Conclusion

In this tutorial, I have provided an in-depth explanation of finding the sum of an array in Python I have explained how to create an Array in Python. I also discussed various ways of finding the sum of an array in Python like using sum() function is a simple way, to use a for loop to iterate over each element, using list comprehension. we have also discussed how to handle large arrays in Python.

Hope this article has helped you to understand more about finding the sum of an array in Python.

You may also 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.