Register Login

How to Find the Sum of Elements in a List in Python

28 Jul 2020 11:40 am || 0

In Python, programmers work with a lot of lists. Sometimes, it is necessary to find out the sum of the elements of the lists for other operations within the program.

In this article, we will take a look at the following ways to calculate sum of all elements in a Python list:

1) Using sum() Method

Python provides an inbuilt function called sum() which sums up the numbers in a list.

Syntax

Sum(iterable, start)
  • Iterable – It can be a list, a tuple or a dictionary. Items of the iterable have to be numbers.  
  • Start – This number is added to the resultant sum of items. The default value is 0.

The method adds the start and the iterable elements from left to right.

Example:

sum(list)
sum(list, start)

Code Example:

# Python code to explain working on sum() method
# Declare list of numbers
numlist = [2,4,2,5,7,9,23,4,5]
numsum = sum(numlist)
print('Sum of List: ',numsum)
# Example with start
numsum = sum(numlist, 5)
print('Sum of List: ',numsum)

Output:

Sum of List:  61
Sum of List:  66

Explanation 

Here, you can see that the sum() method takes two parameters – numlist, the iterable and 5 as the start value. The final value is 61 (without the start value) and 66 (with the start value 5 added to it).  

2) Using for Loop

# Python code to calculate sum of integer list
# Using for loop
# Declare list of numbers
numlist = [2,4,2,5,7,9,23,4,5]
# Calculate sum of list 
numsum=0
for i in numlist:
    numsum+=i
print('Sum of List: ',numsum)

Output

Sum of List:  61

Explanation

Here, a for loop is run over the list called numlist. With each iteration, the elements of the list are added. The result is 61 which is printed using the print statement.

3) Sum of List Containing String Value

# Python code to calculate sum of list containing integer as string
# Using for loop
# Declare list of numbers as string
numlist = ['2','4','2','5','7','9','23','4','5']
# Calculate sum of list
numsum=0
for i in numlist:
    numsum+=int(i)
print('Sum of List: ',numsum)

Output

Sum of List:  61

Here, the list called numlist contains integers as strings. Inside the for loop, these string elements are added together after converting them into integers, using the int() method.

4) Using While Loop

# Python code to calculate sum of list containing integer as string
# Using While loop
# Declare list of numbers as string
numlist = [2,4,2,5,7,9,23,4,5]
# Declare function to calculate sum of given list
def listsum(numlist):
    total = 0
    i = 0
    while i < len(numlist):
        total = total + numlist[i]
        i = i + 1
    return total
# Call Function
# Print sum of list
totalsum = listsum(numlist);
print('Sum of List: ', totalsum)

Explanation

In this program, elements of the numlist array are added using a while loop. The loop runs until the variable i is less than the length of the numlist array. The final summation is printed using the value assigned in the totalsum variable.

Conclusion

Using a for loop or while loop is great for summing elements of a list. But the sum() method is faster when you are handling huge lists of elements.