Python list allows you to store a list of numbers in it.
You can sum a list of numbers in Python using the sum(list) statement.
In this tutorial, you’ll learn how to sum a list of numbers in Python and when it is appropriate to each method.
If You’re in Hurry…
You can use the below code to sum a list of numbers in Python.
num_list = [10, 20,30,30,40,50]
sum(num_list)
Python list allows you to store different types of objects in the same list. Hence, if there is a non-number item in this list, then you’ll get the error when you use the sum()
function. Read below to understand better.
Output
180
If You Want to Understand Details, Read on…
There are native list methods available and also you can create a function using the for
loop to sum a list of numbers. Let us learn these methods in detail.
Table of Contents
Using Sum()
You can use the sum() function to sum the list of numbers in Python.
It is appropriate to use this function when you’re sure that the list contains ONLY numbers.
Code
The below code demonstrates how to use the sum() function to sum the list of numbers.
num_list = [10, 20,30,30,40,50]
sum(num_list)
Output
All the numbers in the list are summed and then the output is displayed.
180
Now let us see what happens when your list contains a String item in it.
Code
The list in the below code contains a String item i
along with other 6 numbers. When you invoke the sum()
function on this list, an unsupported operand type error will occur.
num_list = [10, 20,30,30,40,50,'i']
sum(num_list)
Output
The below error is thrown as the String item i
is available in the list.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
1 num_list = [10, 20,30,30,40,50,'i']
2
----> 3 sum(num_list)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
In this case, you need to create your own methods to sum only the numbers available in the list.
Using For Loop
In this section, you’ll learn how you can sum a list of numbers using the for
loop.
To sum up a list of numbers,
- Declare a variable to store the sum as
sum_of_nums
- Iterate over the list items using the
for
loop - Add it to the sum variable
sum_of_nums
- Once the iteration is complete, you’ll have the sum of the list in this variable.
Code
The below code demonstrates how to use the for
loop to sum a list of numbers.
num_list = [10, 20,30,30,40,50]
sum_of_nums = 0
for i in num_list:
sum_of_nums += i
sum_of_nums
The numbers in the list are summed and the output is displayed.
Output
180
Code
The code below demonstrates how to use the for
loop to sum ONLY the numbers in the list. The other item types like String will be ignored.
During each iteration, you can use the isinstance() method to check if the current item is an instance of numbers.Number
. Only if it is a Number
, then it’ll be summed. Otherwise, the item will be ignored.
This is how you can use the for
loop to sum only the numbers in a list.
num_list = [10, 20,30,30,40,50,'i']
sum_of_nums = 0
for i in num_list:
if isinstance(i, numbers.Number):
sum_of_nums += i
sum_of_nums
Output
The string i
in the list is ignored and the other numbers are summed.
180
Summing Only Numbers available In List
This section teaches you how to sum only the numbers available in the list using the sum()
function and the list comprehension.
Using list comprehension, you can check if the current item in the list is an instance of Number
. If it is a number, then you can pass the item to the sum()
function.
Hence, the sum()
function here will not show an error while summing the list
with the String
items.
Code
import numbers
num_list = [10, 20,30,30,40,50,'i']
sum(item for item in num_list if isinstance(item, numbers.Number))
Output
Only the numbers in the list are summed and the output is displayed.
180
Sum Numbers Stored as String
In some cases, the numbers will be stored as strings in a list in python.
In this case, you have to convert these String-type numbers to an integer using int()
and then pass it to the sum()
function.
Code
num_list = ['10', '20','30','30','40','50']
sum(int(i) for i in num_list)
Output
The String-type numbers are converted to int
and summed together using the sum()
function.
180
Conclusion
You’ve learned how to sum a list of numbers in Python using the sum()
function and using a user-defined function with for loops.
Also, the python list
can contain numbers and String-type items in the same list. You have learned how to sum only the number type items in the list using list comprehension.
If you have any questions, please comment below.