Find sum of elements in List - Python
Last Updated : 05 May, 2025
Finding the sum of elements in a list means adding all the values together to get a single total. For example, given a list like [10, 20, 30, 40, 50], you might want to calculate the total sum, which is 150. Let's explore these different methods to do this efficiently.
Using sum()
sum() function is the most efficient way to calculate the sum of all elements in a list. It automatically takes care of iterating over the list and summing up the values for you.
Python a = [10, 20, 30, 40, 50] res = sum(a) print(res)
Explanation: sum() function adds all the numbers in the list a and stores the result in res.
Using for loop
For loop gives you more control over the process. By manually iterating over the list, you can accumulate the sum step by step.
Python a = [10, 20, 30, 40, 50] res = 0 # Initialize sum variable for i in a: res += i print(res)
Explanation: For loop iterates through each element in the list, adding it to res in each step until all elements are summed.
Using list comprehension
List comprehension allows you to create a new list based on an existing one and then use sum() to calculate the total. This is a more Pythonic way to do things when you want to transform the list first.
Python a = [10, 20, 30, 40, 50] res = sum([i for i in a]) print(res)
Explanation: List comprehension creates a new list identical to a and the sum() function then calculates the total of its elements, storing the result in res.
Using reduce()
reduce() function from the functools module applies a function (in this case, addition) to the elements of the list cumulatively. This method is less commonly used but provides a functional programming style solution.
Python from functools import reduce a = [10, 20, 30, 40, 50] res = reduce(lambda x, y: x + y, a) print(res)
Explanation: reduce() function uses the lambda to cumulatively add pairs of elements from left to right, resulting in the total sum stored in res.
Related articles
Similar Reads
Index of Non-Zero Elements in Python list We are given a list we need to find all indexes of Non-Zero elements. For example, a = [0, 3, 0, 5, 8, 0, 2] we need to return all indexes of non-zero elements so that output should be [1, 3, 4, 6].Using List ComprehensionList comprehension can be used to find the indices of non-zero elements by ite
2 min read
Python - Find all elements count in list In Python, counting the occurrences of all elements in a list is to determine how many times each unique element appears in the list. In this article, we will explore different methods to achieve this. The collections.Counter class is specifically designed for counting hashable objects. It provides
3 min read
Python | Indices of Kth element value Sometimes, while working with records, we might have a problem in which we need to find all the indices of elements for a particular value at a particular Kth position of tuple. This seems to be a peculiar problem but while working with many keys in records, we encounter this problem. Let's discuss
4 min read
Get Index of Multiple List Elements in Python In Python, retrieving the indices of specific elements in a list is a common task that programmers often encounter. There are several methods to achieve this, each with its own advantages and use cases. In this article, we will explore some different approaches to get the index of multiple list elem
3 min read
Check if element exists in list in Python In this article, we will explore various methods to check if element exists in list in Python. The simplest way to check for the presence of an element in a list is using the in Keyword. Example:Pythona = [10, 20, 30, 40, 50] # Check if 30 exists in the list if 30 in a: print("Element exists in the
3 min read
How to Find Length of a list in Python The length of a list means the number of elements it contains. In-Built len() function can be used to find the length of an object by passing the object within the parentheses. Here is the Python example to find the length of a list using len().Pythona1 = [10, 50, 30, 40] n = len(a1) print("Size of
2 min read