How to find the sum of the list in Python

In this Python tutorial, we will see how to find the sum of the list in Python using different methods with demonstrative examples.

A list in Python is an ordered sequence of items that can contain a mix of different data types, including numbers, strings, and other lists. Lists are mutable, meaning we can change their elements. They are defined by enclosing a comma-separated sequence of elements within square brackets [].

For example, we a have list of numbers:

Numbers = [4, 5, 8, 1, 0, 2, 6, 9]

What if we want to sum of all these numbers.

The sum of the list in Python

There are various ways to find the sum of the list in Python. They are:

  • the sum() Function
  • for Loop
  • List Comprehension
  • while Loop
  • Using Recursion Function

We will see them one by one using illustrative examples.

The sum of the list in Python using the sum() function

The simplest way to find the sum of a list in Python is by using the built-in sum() function.

For example, Suppose we have a list of the tallest buildings in New York City, measured in feet: Here, the sum() function computes the total height of the tallest buildings in New York City. The function accepts an iterable (like our list) and adds up all the numbers in the iterable.

NYC_buildings = [1454, 1250, 1046, 1046, 1007]

total_height = sum(NYC_buildings)

print('The total height of NYC buildings(in feet) will be: ',total_height)

The output is:

The total height of NYC buildings(in feet) will be:  5803
sum of the list in Python using sum() function

This way we can use the sum() function to find the sum of the list in Python.

The sum of the Python list using for loop

We can also use a for loop in Python to calculate the sum of elements in a list.

Here’s an example where we find the sum of the populations (in millions) of the five most populous states in the USA which is stored in a form of a Python list. In this case, we iterate over each population figure in the Python list and add it to the running total.:

# California, Texas, Florida, New York, Pennsylvania

populations = [39.5, 29.5, 21.8, 20.0, 19.9]
total_population = 0

for population in populations:
    total_population += population

print(total_population)

The Output is:

130.7
sum of the list in Python using for loop

This way we can use for loop to find the sum of the list in Python.

The sum of the list in Python using list comprehension

List comprehension in Python is a compact way to process all elements in a list.

Let’s see how to use list comprehension with a Python list of the lengths (in miles) of the five longest rivers in the US: Here, the sum is computed on the fly within the list comprehension statement.

# Missouri, Mississippi, Yukon, etc.
US_river_lengths = [2530, 2460, 2320, 2170, 2078]

total_length = sum(river_length for river_length in US_river_lengths)

print('The total length of the rivers(in miles) is: ', total_length)

The output is:

The total length of the rivers(in miles) is:  11558
sum of the list in Python using list comprehension

This way we can use the list comprehension to find the sum of the list in Python.

The sum of the list in Python using while loop

The while loop is another control flow statement in Python that can be used to find the sum of a list.

For example, consider the case where we want to find the sum of the areas (in sq miles) of the top five largest states in the U.S which stored in the form of a Python List: In this case, we loop through each state’s area in the Python list and add it to the total until we’ve covered all elements.

# Alaska, Texas, California, Montana, New Mexico
largest_states = [663267, 570641, 268596, 261231, 195303]
total_area = 0
i = 0

while i < len(largest_states):
    total_area += largest_states[i]
    i += 1

print('Total areas (in sq miles) of the top five largest states in the U.S:', total_area)

The output is:

Total areas (in sq miles) of the top five largest states in the U.S: 1959038
sum of the list in Python using while loop

This way we can use while loop to find the sum of the list in Python.

The sum of the list in Python using recursion function

If we want to practice recursion function, we can use it to find the sum of a list in Python.

Here’s an example where we find the sum of the number of National Parks in the five states with the most National Parks which is stored in a Python list:

This recursive function in Python takes a list as input and returns the sum of the elements. It adds the first element of the Python list to the sum of the rest of the list until the list is empty.

# California, Alaska, Utah, Colorado, Arizona
national_parks = [24, 13, 10, 8, 7]

def find_sum(lst):
    if len(lst) == 0:
        return 0
    else:
        return lst[0] + find_sum(lst[1:])

total_parks = find_sum(national_parks)
print(total_parks)

The Output is:

Total numbers of parks:  62
sum of the list in Python using recursion

This way we can use the recursion function in Python to find the sum of the list.

Conclusion

Python offers a multitude of methods to find the sum of a list. From built-in functions like sum(), loops such as for and while, list comprehension, and recursion function methods. Each method has its unique use cases and benefits. The method we choose should align with our specific requirements, our data structure, and our task’s performance needs.

You may like also like to read: