How To Sum a list of numbers in Python – Definitive Guide

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.

Basic Example

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, you’ll get the error when you use the sum() function. Read below to understand better.

Output

    180

In this tutorial, you’ll learn how to sum a list of numbers in Python and when it is appropriate to each method.

Using Sum()

The sum() function sums the list of numbers in the Python list.

Use this function when you’re sure that the list contains ONLY numbers.

Code

num_list = [10, 20,30,30,40,50]

sum(num_list)

Output

All the numbers in the list are summed, and 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 following 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,

  1. Declare a variable to store the sum as sum_of_nums
  2. Iterate over the list items using the for loop
  3. Add it to the sum variable sum_of_nums
  4. 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,

  • use the isinstance() method to check if the current item is an instance of numbers.Number
  • Only if it is a Number, it’ll be summed. Otherwise, the item will be ignored
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,

  • Check if the current item in the list is an instance of a Number.
  • If it is a number, pass the item to the sum() function.

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.

  • Convert these String type numbers to an integer using int()
  • 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

Additional Resources

Leave a Comment