Find Sum of All Elements in List in Python

Nov 28, 2022

.

Admin

Find Sum of All Elements in List in Python

Hi Dev,

This tutorial is focused on find sum of all elements in list in python. This article goes in detailed on how to get sum of elements in list in python. This tutorial will give you simple example of python sum all elements in list of list. you will learn how to find sum of all elements in list in python.

There are many ways to get a sum of elements in a list in python. i will give you two examples using sum() method and for loop to sum of all elements of list in python. so let's see the below examples.

so let's see following examples with output:

Example 1: using sum() Method


main.py

myList = [10, 5, 23, 25, 14, 80, 71]

# Sum of elements to list

total = sum(myList)

print(total)

Output:

228

Example 2: using For Loop

main.py

myList = [10, 5, 23, 25, 14, 80, 71]

# Sum of elements to list

def getSumOfElement(list):

total = 0

for val in list:

total = total + val

return total

total = getSumOfElement(myList)

print(total)

Output:

228

#Python