
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Sum of Elements in List using Python
In this article, lets see try to find the sum of elements in a list. For a given integer list, the program should return the sum of all elements of the list. For an example let's consider list_1 = [1,2,3,4,5] the output of the program should be 15.
Following are the different types of approaches to find the sum of elements in a list ?
- Iterating through loops
- Using Recursion
- Using sum() function
- By Importing operator Module
- Using enumerate Function
Iterating Through Loops
We can find the sum of elements in a list using loops. In this method, we need to initialize a variable to zero and iterate through the list, adding each element to the initialized variable. By the end of the loop, the variable will contain the sum of all the elements in the list.
Example
Following is an example of finding the sum of the elements in a list using loops ?
#Creating a list my_List = [10,20,30,40,50] list_sum = 0 for i in my_List: list_sum = list_sum + i print("Sum of all elements of the list :",list_sum)
Output
Following is the output of the above code ?
Sum of all elements of the list : 150
Using Recursion
The function which calls itself is known as a recursive function. Using a recursive function, we can find the sum of all elements in a list by breaking the problem down into smaller subproblems.
In this approach, the function adds the last element of the list to the sum of the remaining elements by calling itself a smaller portion of the list, decrementing the size each time. The recursion continues until the size becomes zero, at which point the function returns zero, and the accumulated sum is returned as the result.
Example
In the following example we have found the sum of all the elements in the list using recursion ?
# elements in list using recursion # creating a list my_list = [101, 75, 87, 8, 28] # creating sum_list function def Sum_Of_List(list, size): if (size == 0): return 0 else: return list[size - 1] + Sum_Of_List(list, size - 1) # Driver code Sum = Sum_Of_List(my_list, len(my_list)) print("Sum of all elements in given list: ", Sum)
Output
Following is the output of the above code ?
Sum of all elements in given list: 299
Using 'sum()' Function
In Python, the sum() is an in-built function used to find the sum of the given sequence. It takes an iterable such as a list, tuple, or set and returns the total sum of its elements. The sum() function can also take an optional second argument, which serves as the starting value for the sum. By default, this value is zero, but we can change it to any other number if needed.
Example
Following is an example of finding sum of elements using the sum() function in a list ?
#Creating a list my_list = [15,79,4,53,67] #sum() funtion Sum_Of_List = sum(my_list) print("Sum of all elements in a list :",Sum_Of_List)
Output
Following is the output of the above code ?
Sum of all elements in a list : 218
By Importing 'operator' Module
The operator module consists of the add() function, which accepts two arguments and returns the sum of the given arguments. To use it, we need to import the operator module using the import keyword. We can find the sum of elements in a list using the add() function.
Example
Here, is a usage of the operator module to find the sum of elements in a list ?
from operator import* my_list = [85, 65, 43, 17] Sum = 0 for i in my_list: # Adding elements in the list using # add function of operator module Sum = add(i, Sum) # printing the result print("Sum of all elements in a list :",Sum)
Output
Following is the output of the above code ?
Sum of all elements in a list : 210
Using 'enumerate' Function
In Python, the enumerate() function adds a counter to an iterable (such as a list, tuple, or string) and returns it as an enumerated object. This object can then be converted into a list of tuples, where each tuple contains an index and the corresponding item from the original iterable. Using this function we can also find the sum of all elements in a list.
Example
Following is an example of an enumerate function to find the sum of elements in a list ?
list1 = [72, 205, 13, 56] Sum = 0 for i,ele in enumerate(list1): Sum+=ele print("Sum of all elements in a given list :",Sum)
Output
Following is the output of the above code ?
Sum of all elements in a given list : 346