Javaexercise.com

How To Get Sum Of List Elements In Python

In this article, we are going to see how we can find the sum of elements present in the list. Let's understand the topic with the help of simple examples.

For Examples:

Input: [2,4,6,8]
2+4+6+8 =20
Ouput: 20
​
Input: [9,5,7,8,6]
9+5+7+8+6 =35
Ouput: 35

The different ways to calculate the sum of elements present in the list are:

  1. Using for loop

  2. Using sum() function

  3. Using while loop

  4. Sum of the list containing string values

Find the sum of list elements using for loop

In this method, we will use a loop for accessing each element of the list, and then we will add the elements individually.

Algorithm:

Step 1: Declare a variable sum and set its initial value 0

Step 2: Create the list

Step 3: Run a for loop to access each element

Step 4: Add each element of the list to the sum

Step 5: Print the sum

See the below Python code implementation of the above algorithm.

# Python program to find the sum of elements in the list
sum = 0
​
# creating a list
list1 = [10, 5, 20, 15, 30]
​
# Iterate each element in list
# and add them in variable sum
for i in range(0, len(list1)):
    sum = sum + list1[i]
​
# printing the sum
print(f"Sum of the elements in the given list: {sum}")

​Output:

Sum of the elements in the given list: 80

Find the sum of list elements using sum() function

In this method, we will use the built-in function sum() which will calculate the sum of all the elements present in the array and return sum.

Algorithm:

Step 1 : Initialize the list

Step 2 : Use the sum() function

Step 3 : Print the sum

See the below Python code implementation of the above algorithm.

# Python program to find the sum of elements in the list
​
# creating a list
list1 = [10, 5, 20, 15, 30]
​
# using sum() function
total = sum(list1)
​
# printing total value
print("Sum of all elements in given list: ", total)

​Output:

Sum of all elements in given list: 80

Find the sum of list elements using while loop

Algorithm:

Step 1: Declare a variable for storing the sum

Step 2: Declare another variable and set it to 0.

Step 3: Create the list

Step 4: Run while loop to access each element

Step 5: Add each element of list to the sum

Step 6: Add 1 to that variable each time when the loop runs

Step 7: Print the sum

See the below Python code implementation of the above algorithm.

# Python program to find the sum of elements in the list
sum = 0
ele = 0
​
# creating a list
list1 = [10, 5, 20, 15, 30]
​
# Iterate each element in list
# and add them in variable sum
while(ele < len(list1)):
    sum = sum + list1[ele]
    ele += 1
​
# printing total value
print("Sum of all elements in given list: ", sum)

​Output:

Sum of all elements in given list: 80

Find the sum of list elements if list containing string values

Algorithm:

Step 1: Declare a variable for storing the sum

Step 2: Create the list

Step 3: Run a for loop to access each element

Step 4: Use int() method to convert the string into integers and add them in the sum

Step 5 : Print the sum

See the below Python code implementation of the above algorithm.

# Python code to calculate sum of list containing integer as a string
# Using for loop
​
# Declare a variable and set it to 0
numsum = 0
​
# Create a list of numbers as string
numlist = ['10', '5', '20', '15', '30']
​
​
# Iterate each element in list
# and add them in variable sum
for i in numlist:
    numsum += int(i)
​
# print the sum
print('Sum of all the element in given list: ', numsum)

​Output:

Sum of all the element in given list: 80

Conclusion:

In this tutorial, we have learned different methods for calculating the sum of elements present in the list. We can use for loop, while loop, and sum() method to achieve it. But the sum() method is faster when we have to handle the huge lists of elements.