Summing All Numbers in a List | Python

Lists are quite useful Data Type in Python Programming Language, these can be used for holding a lot of data as a single entity and also these are mutable which makes Python List more useful.
In this article, I’ll be discussing ways to sum up all numbers in a Python List. Typically there exists four ways for summing elements in a List in Python.

  • Summing Elements of List using For Loop
  • Summing up all Elements in List using While Loop
  • Using Recursion for finding sum of all elements in a List
  • Using Python’s sum() Function for Finding what is total sum of elements in a Python List

Let’s discuss each one of these four steps.

Using For Loop to find sum of elements in List

# Python program to find sum of elements in list
total_sum = 0

# creating a list
a_list = [36, 82, 291, 83, 8]

# Iterate each element in list
# and add them in variale total_sum
for element in range(0, len(a_list)):
	total_sum = total_sum + list1[ele]

# printing sum of all elements in List
print("Sum of all elements in list => ", total_sum)

Output of Above Code

Sum of all elements in list =>  500

Using while loop to find sum of elements in List

# Python program to find sum of elements in list
total_sum = 0
n = 0

# creating a list
a_list = [717, 92, 73, 2819, 27]

# Iterate each element in list
# and add them in variale total
while(n < len(a_list)):
	total_sum = total_sum + a_list[n]
	n += 1
	
# printing total value
print("Sum of all elements in list => ", total_sum)

Output of Above Code

Sum of all elements in list =>  3728

Using Recursion for finding sum of all elements in a List

# Python program to find sum of all elements in list using recursion

# creating a list
a_list = [17, 29, 182, 291]

# creating sum_list function
def sumofelements(a_list, size):
	if (size == 0):
		return 0
	else:
		return a_list[size - 1] + sumofelements(a_list, size - 1)  # Recursion

# Calling sumofelements function	
total_sum = sumofelements(a_list, len(a_list))

print("Sum of all elements in given list: ", total_sum)

Output of Above Code

Sum of all elements in given list =>  519

Python’s sum() Function for Summing Up Elements of a List

# Python program to find sum of elements in list using sum() Function

# creating a list
a_list = [728, 29, 1762, 1783, 7543]

# using sum() function
total_sum = sum(a_list)

# printing total_sum value
print("Sum of all elements in given list => ", total_sum)

Output of Above Code

Sum of all elements in given list =>  11845

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts