How to Sum Elements in List in Python using For Loop

In this Python tutorial, we will learn how to sum elements in a list in Python using for loop with a few examples.

Lists in Python

In Python, a list is a data structure that stores multiple items in a single variable. Lists are created by placing all the items inside square brackets [], separated by commas. It can have any number of items and they may be of different types (integer, float, string, etc.).

For example, here is a list of integers:

numbers = [1, 2, 3, 4, 5]

For Loop in Python

The for loop in Python is used to iterate over a sequence (like a list, tuple, dictionary, set, or string) or other iterable objects. Iterating over a sequence is called traversal.

Here is an example of a for loop:

for number in numbers:
    print(number)

Various Approaches to Sum Elements in a List Using a For Loop

Using For Loop in Python

The most straightforward way to sum elements in a list using a Python for loop involves initializing a variable to store the sum, then iterating over the list and adding each element to this variable.

Consider a list of the highest temperatures ever recorded in five US states (in degrees Fahrenheit):

temperatures = [134, 128, 124, 118, 115]  # CA, AZ, NV, TX, KS respectively

total = 0
for temperature in temperatures:
    total += temperature
print("The sum of the highest recorded temperatures is: ", total)

This example uses the highest recorded temperatures from five different states. The Python for loop iterates through each temperature in the list and adds it to the total variable. The final print statement displays the sum of these temperatures.

READ:  How to Return Function Name in Python

Output:

How to Sum Elements in List in Python using For Loop

Read How to check if the list is empty in Python

Using Enumerate in Python

The Python enumerate() function can be used in a for loop to loop over something and have an automatic counter. Here’s how you can use it for summing elements in a list. Consider a list representing the populations (in millions) of five major US cities.

populations = [8.4, 4.0, 2.7, 2.7, 1.7]  # NYC, LA, Chicago, Houston, Phoenix respectively

total = 0
for i, population in enumerate(populations):
    total += population
print("The total population of the five cities is: ", total, "million")

This example uses the populations of five major US cities. The Python enumerate function is used to get an index (i) and the value from the list (population) for each iteration of the Python for loop. We only use population to add to the total. The final print statement shows the total population of these cities.

Output:

How to Sum a List in Python using For Loop

Read: How to find the sum of the list in Python

Using For Loop with Range in Python

Another approach to sum elements in a list using a Python for loop is to use the Python range() function, which generates a sequence of numbers. Here’s how you can do it.

Consider a list of the lengths (in miles) of the five longest rivers in the US.

rivers = [2530, 2460, 2348, 2320, 2068]  # Missouri, Mississippi, Yukon, Rio Grande, Colorado respectively

total = 0
for i in range(len(rivers)):
    total += rivers[i]
print("The total length of the five longest rivers is: ", total, "miles")

Here, we have a list of the lengths of the five longest rivers in the US. The Python for loop, combined with the Python range() function and len() function, generates an index (i) for each iteration. This index is used to access the value from the list (rivers[i]) which is then added to the total. The final print statement shows the total length of these rivers.

READ:  Python Dictionary Append: How to add key-value pair

Output:

sum list of element in python using for loop

Read: How to loop through a list in Python

Using For Loop with List Slicing

You can also use list slicing within a Python for loop to sum elements in a list. Here’s an example: Consider a list representing the number of national parks in several US states.

national_parks = [9, 8, 5, 4, 4]  # California, Alaska, Utah, Colorado, Washington respectively

total = 0
for park in national_parks[:]:  # A copy of the list is made.
    total += park
print("The total number of national parks in these states is: ", total)

This example uses a list of the number of national parks in several states. The Python for loop iterates over a copy of the original list (created by slicing with [:]), adding each number to the total.

This method is especially useful if you need to modify the original list while iterating over it. The final print statement shows the total number of national parks in these states.

Output:

sum of list of element using for loop in python

Conclusion

Understanding the basics of Python like lists and for loops allows you to tackle a wide range of problems. While Python does provide built-in functions for common operations like summing the elements in a list, it’s beneficial to understand how these operations work on a fundamental level.

In this article, we’ve seen how we can use a for loop to sum the elements in a list manually You may like the following Python tutorials: