Code Example: How To Add Numbers In A List Python

Are you looking for a program in Python on how to add numbers in a list? This article will show you different methods and their runtime that you can use to add the numbers to a list.

In python, you can simply use the sum method if a list of numbers is passed as an argument to the function. Though there are other methods as well that you can use one such is using the for loop or while loop.

Add Numbers In a List Using Sum Method in Python

Let us see in the below example code the usage of the sum method that you can use to get the sum of a list instantly.

#initializing the List of Number
numbersToAdd = [1,2,6,17,19,31,45,14]

#Adding the Numbers using Sum Method
sumOfList = sum(numbersToAdd)

#Printing the Sum of Numbers
print("Total Sum of the List is = {}".format(sumOfList))

Output:

Total Sum of the List is = 135

Using For Loop to Add Numbers of a List in Python

You can use the For loop as well to add all the numbers in the Python list. This method can be really handy when you have a list containing Numbers and non-numbers and you only want to add the numbers found in the list.

Though you can use the for loop to find the sum for the normal list of numbers as well. Let us see in the below two code examples for For Loop.

#initializing the List of Number
numbersToAdd = [1,2,6,17,19,31,45,14]

#Adding the Numbers using For Loop
totalSum = 0
for index in range(0, len(numbersToAdd)):
    totalSum += numbersToAdd[index]

#Printing the Sum of Numbers
print("Total Sum of the List is = {}".format(totalSum))

Output:

Total Sum of the List is = 135

Now modifying the above code when you have a list containing alphanumeric characters and you want to add only numbers.

#initializing the List of Number
numbersToAdd = ['R',1,2,'E',6,'A',17,19,'B',31,'C',45,14]

#Adding the Numbers using For Loop
totalSum = 0
for index in range(0, len(numbersToAdd)):
    if str(numbersToAdd[index]).isnumeric():
        totalSum += numbersToAdd[index]

#Printing the Sum of Numbers
print("Total Sum of the List is = {}".format(totalSum))

Output:

Total Sum of the List is = 135

Using While Loop To Add Numbers Of A List

You can use the while loop as well to add the numbers. Let us see in the below example code the usage of while to add numbers from a list in Python.

#initializing the List of Number
numbersToAdd = [1,2,6,17,19,31,45,14]

#Adding the Numbers using While Loop
totalSum = 0
index = len(numbersToAdd)-1
while index>=0:
    totalSum += numbersToAdd[index]
    index = index - 1


#Printing the Sum of Numbers
print("Total Sum of the List is = {}".format(totalSum))

Output:

Total Sum of the List is = 135

In the while loop, you need to create a variable to indicate the index of the list. And we are decreasing the index till we reach the start of the index in the above code.

Using Recursion in Python to Add the Numbers of A List

You can use the recursion as well to add the list of a number given. This method will be called recursively till the length of the given array of numbers or list of numbers in Python. Let us see in the below example code the usage of recursion.

#initializing the List of Number
numbersToAdd = [1,2,6,17,19,31,45,14]

#Adding the Numbers using Recursion
def recursiveSum(giveNumberList, len):
    #Return 0 when Reaches End of List
    if len<0: 
        return 0
    return giveNumberList[len] + recursiveSum(giveNumberList, len-1)

totalSum = recursiveSum(numbersToAdd, len(numbersToAdd)-1)

#Printing the Sum of Numbers
print("Total Sum of the List is = {}".format(totalSum))

Output:

Total Sum of the List is = 135
Code Example: How To Add Numbers In A List Python

Adding Numbers From User Input as A List in Python

If you want to take the list of numbers as input from your user then you need to follow the below code example do to so.

#Taking List of Number as Input from User
inputNumbers = int(input("How Many Numbers to be Added.\n"))

#Initializing the Empty Number List
numberList = []

#Appending Numbers from User
for i in range(0, inputNumbers):
    numberList.append(int(input("Enter the number.\n")))


#Adding the Numbers using sum
totalSum = sum(numberList)

#Printing the Sum of Numbers
print("Total Sum of the List is = {}".format(totalSum))

Output:

How Many Numbers to be Added.
5
Enter the number.
1
Enter the number.
2
Enter the number.
3
Enter the number.
4
Enter the number.
5
Total Sum of the List is = 15

Wrap Up

That’s it for this article. I hope you were able to get the answer related to how to add list of numbers in Python. Let me know if you have any better method than the one discussed above. I will be happy to add here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. Python – How To Delete All Files In A Directory
  2. How To Remove Non-alphanumeric Characters From String In Python
  3. How To Multiply Without Using * In Python
  4. How To Add or Append Values To a Set In Python
  5. 4 Ways To List All Subdirectories in a Directory – Python
  6. Dict To list – Convert A Dictionary to a List in Python
  7. Discord.py Bot Add Reaction To a Message Python
  8. Python Get The Minimum Value In Dictionary with Key
  9. How To Append Multiple Values To A List in Python
  10. How To Reboot or Restart Python Script

Leave a Reply

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