How to find the sum of elements in the list in Python? There are different ways to calculate the sum of all the number elements in the list. for example, you can use the sum() built-in function.
Below are the methods to calculate the sum of elements from the list in Python.
- Method 1: Using Looping like for loop and while loop.
- Method 2: Using sum() built-in function
- Method 3: List Comprehension e.t.c
1. Quick Examples of Getting Sum of Elements in a List
Following are quick examples of how to find the sum of elements in the List.
# Quick Examples of getting sum of elements in list
# Consider the list of integers
mylist1=[20,40,32,6,78,90]
# Using sum()
print("SUM: ", sum(mylist1))
# Using sum() start from 50
print("SUM after 50: ", sum(mylist1,50))
# Using List Comprehension with sum()
print("SUM: ", sum([i for i in mylist1]))
# Using for loop with range
total=0
for i in range(len(mylist1)):
total=total+mylist1[i]
print("SUM: ", total)
# Using for loop
total=0
for i in mylist1:
total=total+i
print("SUM: ", total)
# Using add() with for loop
from operator import add
total=0
for i in mylist1:
total = add(i, total)
print("SUM: ", total)
# Using while loop
total=0
i=0
while (i < len(mylist1)):
total=total+mylist1[i]
i=i+1
print("SUM: ", total)
2. Sum of List Elements in Python using sum()
The sum() is the built-in function in python that is used to get or find the sum of all numeric elements from list. By using this you can also specify an initial value to start the calculating sum.
Related: Calculate the Average of Elements in Python List
It takes two parameters as input and returns the total sum of elements in the list. The first parameter is the input list and the second parameter specified the start value in which the sum is computed from this value. This parameter is optional.
4.1 sum() Syntax
Let’s see the syntax of sum() function.
sum(mylist1,start)
# Using List Comprehension with sum()
sum([iterator for iterator in mylist1]
# Using List Comprehension with sum()
sum(list(filter(lambda iterator : (iterator ),mylist1)))
4.2 sum() Parameters
mylist1
is the input liststart
takes the integer value which will start summing from this value.
4.3 Python List sum() Examples
Example 1: Let’s have a list of integers and return the sum of all elements in the list using the python sum() function. Here, we didn’t specify the second parameter. So it used all elements from the list to calculate the sum and the total sum is 266.
# Consider the list of integers
mylist1=[20,40,32,6,78,90]
# Using sum()
print("SUM: ", sum(mylist1))
# Output:
# SUM: 266
Example 2: Let’s have a list of integers and return the sum of all elements by specifying the initial value.
Here, we specified the start as 10. So, initially the sum = 10, and then it adds each element to get the sum value 266. The final sum is 10+266=276.
And, in the second example, we specified the start as 50. So, initially, the sum = 50, and the sum of elements in the list gets 266. The final sum is 50+266=316.
# Consider the list of integers
mylist1=[20,40,32,6,78,90]
# Using sum() start from 10
print("SUM after 10: ", sum(mylist1,10))
# Using sum() start from 50
print("SUM after 50: ", sum(mylist1,50))
# Output:
# SUM after 10: 276
# SUM after 50: 316
3. Using List Comprehension to get Sum of List Elements
Let’s have a list of integers and return the sum of all elements in the list using python sum() by passing List Comprehension as a parameter. With python list comprehension, we can create lists by specifying the elements. We select elements that we want to include, along with any conditions or operations. All this is done in a single line of code. The below example calculates the sum of numbers from the list using comprehension.
# Consider the list of integers
mylist1=[20,40,32,6,78,90]
# Using List Comprehension with sum()
print("SUM: ", sum([i for i in mylist1]))
# Output:
# SUM: 266
We provided for loop inside the list comprehension and iterating each element in the list. So the sum() function will take each value from the list and return the total sum.
3. Find List Sum using for Loop
The for loop in Python is a control flow statement that is used to execute code repeatedly over a sequence like a string
, list, tuple
, set, range
, or dictionary(dict) type.
3.1 for loop with range
Here, we will use range()
function in for loop to iterate elements in the python list to get a sum of elements. Inside loop, we will access each element using index position and add each element to the total
variable. The sum is stored in this variable. if you wanted to start the initial value, then assign the value you wanted to the total
variable.
# Consider the list of integers
mylist1=[20,40,32,6,78,90]
# Using for loop with range
total=0
for i in range(len(mylist1)):
total=total+mylist1[i]
print("SUM: ", total)
# Output:
# SUM: 266
3.2 for loop
Here, we just use the list without range() function. Here, we will get a value from the list for each iteration and add each element to the variable. The sum is stored in this variable.
# Consider the list of integers
mylist1=[20,40,32,6,78,90]
# Using for loop
total=0
for i in mylist1:
total=total+i
print("SUM: ", total)
# Output:
# SUM: 266
3.3 for loop with add()
The add() method is available in the operator module which will add elements from the list to get a sum of elements. It takes two variables as parameters.
So we can specify this method and pass the iterator as the first parameter and total variable as the second parameter. On iteration second parameter will update and store in result again.
from operator import add
# Consider the list of integers
mylist1=[20,40,32,6,78,90]
# Using add() with for loop
total=0
for i in mylist1:
total = add(i, total)
print("SUM: ", total)
# Output:
# SUM: 266
4. Find List Sum using while Loop
Inside the while loop, we will access each element using the index position and add each element to the total variable. The sum is stored in this variable. Each time we need to increment the iterator till it is less than the length of the list.
Let’s create a list of integers and return the total sum using a while loop.
# Consider the list of integers
mylist1=[20,40,32,6,78,90]
# Using while loop
total=0
i=0
while (i < len(mylist1)):
total=total+mylist1[i]
i=i+1
print("SUM: ", total)
# Output:
# SUM: 266
5. Conclusion
In this article, you have learned different ways to find the sum of list elements in Python. We used for loop to find the sum of all elements present in the list. Next, we saw how to use a while loop to find the sum of elements in the list. After that, we used sum() method to return the total sum of elements directly in the list. It can also be possible to pass the list comprehension as a parameter to this function to return the total sum.