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.

Sum Elements in List in Python using For Loop

Let’s take an example and add the elements of a Python list using a different approach.

Initialize the variable “sum” equal to zero using the below code.

sum = 0

Create a list that contains the population in millions of the top 10 largest cities of the USA such as New York = 8.5, Los Angeles = 3.9, Chicago = 2.7, Houston = 2.3, Phoenix = 1.6, Philadelphia = 1.6, San Antonio = 1.5, San Diego = 1.4, Dallas = 1.3 and San Jose = 1.0 using the below code.

usa_pop = [8.5, 3.9, 2.7, 2.3, 1.6, 1.6, 1.5, 1.4, 1.3, 1.0]

Define the for loop and iterate over the elements of the list “usa_pop” and add them in variable “sum” using the below code.

for element in range(0, len(usa_pop)):
    sum = sum + usa_pop[element]

Check the sum of the elements of the list in the variable “sum” using the below code.

print(sum)
How To Sum Elements In List In Python Using For Loop
How To Sum Elements In List In Python Using For Loop

Sum Elements in List Using add() function in Python For Loop

Under the module “operator,” Python includes predefined functions for a variety of logical, bitwise, relational, and other operations. So in this approach, we will use the add() function of the module operator.

First import the module operator using the below code.

from operator import*

Initialize the variable “sum” equal to zero using the below code.

sum = 0

Create a list that contains the population in millions of the top 10 largest cities of the USA such as New York = 8.5, Los Angeles = 3.9, Chicago = 2.7, Houston = 2.3, Phoenix = 1.6, Philadelphia = 1.6, San Antonio = 1.5, San Diego = 1.4, Dallas = 1.3 and San Jose = 1.0 using the below code.

usa_pop = [8.5, 3.9, 2.7, 2.3, 1.6, 1.6, 1.5, 1.4, 1.3, 1.0]

Define for loop to access each element of the list “usa_pop” and use the add() function to sum the elements of the list using the below code.

for i in usa_pop:
    sum = add(i, 0)+sum

View the sum of the elements of the list in the variable “sum” using the below code.

print(sum)
How To Sum Elements In List In Python Using For Loop Add Function
How To Sum Elements In List In Python Using For Loop Add Function

This is how to sum elements in a list using add() function in Python for loop.

Also, check: How to Python Append List to Another List

Sum Elements in List Using enumerate() function in Python For Loop

Initialize the variable “sum” equal to zero using the below code.

sum = 0

Create a list that contains the population in millions of the top 10 largest cities of the USA such as New York = 8.5, Los Angeles = 3.9, Chicago = 2.7, Houston = 2.3, Phoenix = 1.6, Philadelphia = 1.6, San Antonio = 1.5, San Diego = 1.4, Dallas = 1.3 and San Jose = 1.0 using the below code.

usa_pop = [8.5, 3.9, 2.7, 2.3, 1.6, 1.6, 1.5, 1.4, 1.3, 1.0]

Define for loop to access each element of the list “usa_pop” with enumerate() function to sum the elements of the list using the below code.

for i,d in enumerate(usa_pop):
  sum+=d

View the sum elements of a list in a variable “sum” using the below code.

print(sum)
Sum Elements in List Using enumerate function in Python For Loop
Sum Elements in List Using enumerate function in Python For Loop

This is how to sum elements in a list using enumerate() function in Python For Loop.

Sum Elements in List Using Comprehension in Python For Loop

Generate a list that contains the population in millions of the top 10 largest cities of the USA such as New York = 8.5, Los Angeles = 3.9, Chicago = 2.7, Houston = 2.3, Phoenix = 1.6, Philadelphia = 1.6, San Antonio = 1.5, San Diego = 1.4, Dallas = 1.3 and San Jose = 1.0 using the below code.

usa_pop = [8, 3, 2, 2, 1, 1, 1, 1, 1, 1]

Create for loop to access each element of the list “usa_pop” using list comprehension to sum the elements of the list using the below code.

summ =[i for i in usa_pop]
print(sum(summ))
Sum Elements in List Using Comprehension in Python For Loop
Sum Elements in List Using Comprehension in Python For Loop

Look at the above output, the sum of all the elements in the list is done by the comprehension method in Python.

We have learned about how to compute the sum of the elements that exist within the list using different methods like comprehension, enumerate and add() functions using the for a loop.

You may like the following Python tutorials: