How to Find Sum of All Elements in List in Python?

By Hardik Savani October 30, 2023 Category : Python

Here, I will show you python sum of list elements. I’m going to show you about python list sum of elements. this example will help you how to find sum of list python. This post will give you a simple example of how to find sum of all elements in list in python. You just need to do some steps to do python sum all elements in list of list.

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.

You can use these examples with python3 (Python 3) version.

let's see below a simple example 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

I hope it can help you...

Tags :
Shares