Write a Python Program to find the Sum of Elements in a List using the built-in method, for loop, while loop, functions, with a practical example.
Python Program to Find Sum of Elements in a List
This program allows users to enter the length of a List. Next, we used For Loop to add numbers to the list.
The sum function returns the sum of all the elements in a List.
NumList = [] Number = int(input("Please enter the Total Number of List Elements : ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) total = sum(NumList) print("\n The Sum of All Element in this List is : ", total)
Using the math fsum method
import math numberList = [10, 25, 40, 55, 60, 89, 98] tot = math.fsum(numberList) print(tot)
377.0
Using numpy sum function
import numpy as np numberList = [10, 25, 40, 55, 60, 89, 98] tot = np.sum(numberList) print(tot)
377
Using pandas Series
import pandas as pd numberList = [11, 250, -140, 555, 60, 89, 98] tot = pd.Series(numberList).sum() print(tot)
923
Using the statistics fsum method
import statistics numberList = [11, 250, -340, 955, 690, 829, 98] tot = statistics.fsum(numberList) print(tot)
2493.0
Python Program to Find Sum of Elements in a List without using sum() method
In this Python program, we are using For Loop to iterate each element in this NumList. Inside the loop, we are adding those elements to the total variable.
NumList = [] total = 0 Number = int(input("Please enter the Length : ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) for j in range(Number): total = total + NumList[j] print("\n The Sum of All Element in this List is : ", total)
Please enter the Length : 5
Please enter the Value of 1 Element : 10
Please enter the Value of 2 Element : 20
Please enter the Value of 3 Element : 30
Please enter the Value of 4 Element : 40
Please enter the Value of 5 Element : 55
The Sum of All Element in this List is : 155
Python Program to add all elements of a list using for loop
numberList = [20, 40, 50, 60, 80, 90] tot = 0 for num in numberList: tot += num print(tot)
340
Python Program to Calculate Sum of List Items using While loop
This program to return the sum of list items is the same as the above. We just replaced the For Loop with a While loop.
NumList = [10, 20, -30, -40, 50, 100] total = 0 j = 0 while(j < len(NumList)): total = total + NumList[j] j = j + 1 print(total)
The sum of list items using a while loop output
110
Python Program to find the Sum of Elements in a List using Functions
This program to find the sum of list items is the same as the first example. However, we separated the Python program logic using Functions.
def sum_of_list(NumList): total = 0 for j in range(len(NumList)): total = total + NumList[j] return total NumList = [19, 11, 32, 86, 567, 32, 9] total = sum_of_list(NumList) print(total)
The sum of list items using functions output
756
An alternative or better approach to writing the above code is shown below.
def sum_of_list(numList): total = 0 for n in numList: total += n return total numList = [19, 11, 32, 86, 567, 32, 9] tot = sum_of_list(numList) print(tot)
756
using list comprehension
numberList = [22, 49, 222, -150, 160, 820, 97] tot = sum([n for n in numberList]) print(tot)
1220
using lambda and reduce function
from functools import reduce numberList = [22, 49, -50, 160, 820, 97] tot = reduce(lambda x, y: x + y, numberList) print(tot)
1098
Python Program to find the Sum of Elements in a List using Recursion
This example code uses the if else statement inside a function and calls the method recursively to calculate the list sum.
def sumOfList(numList): if len(numList) == 1: return numList[0] else: return numList[0] + sumOfList(numList[1:]) numList = [129, 211, 302, -86, 567, 32, 9] total = sumOfList(numList) print(total)
1164