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)
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)
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)
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))
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:
- Python loop through a list
- Python for loop index
- For loop vs while loop in Python
- Python while loop continue
- Python While loop condition
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.