Python sum() Function | Sum of a List in Python

How to use the sum() function in Python and Examples

In this tutorial, you will learn how to use the sum function in Python. The sum() function adds all the numbers in an iterable element, such as a list, tuple, set, or dictionary, and returns the sum.

This tutorial shows you how to sum a list of elements or numbers, sum a list of lists, and sum a tuple containing numbers.

The sum() Function Syntax

The syntax of the sum() function is as follows:

sum(iterable, start=0)

The sum() function sums the value of the start argument and items of the iterate table from left to right and returns the sum value.

The sum() Function Parameters

  • Iterable: A required field. It can be a list, a tuple, a dictionary, etc., but the items in an iterate are usually numbers.
  • Start: The value to add to the sum of iterable items. If omitted, it defaults to 0. It is optional.

Python sum() Examples

Example 1: Add up the numbers in a list

This example shows a sample program that uses the sum() function to sum a list of numbers.

The following illustrates an example of a simple program that adds all numbers in a list and returns total:

  1. list = [100, 300, 200, 300, 100, 400, 240, 120, 430]
  2. total = sum(list)
  3. print("The total is", total)

Output:

The total is 2190

As you can see, it takes only one line of code to sum a list of numbers in Python.
Here is another way to get the same result without using the sum() function.

Let's take a look at an example:

  1. #sum a list of numbers using for loop
  2. list = [100, 300, 200, 300, 100, 400, 240, 120, 430]
  3. total = 0
  4. for l in list:
  5. total += l
  6. print("The total is", total)

Here, we used a for loop statement to iterate through the list and add each number to the variable named total. You can also use a while loop statement to get the same result.

Example 2: Adding numbers in a tuple

The following sample code adds up all the numbers in a tuple and returns the total value:

  1. tuple = (240, 320, 120, 230, 130, 140, 440, 220, 420)
  2. total = sum(tuple)
  3. print("The total is", total)

Output:

The total is 2260

Example 3: Add all the numbers in a list and other numbers

It is also possible to add all the numbers in the list and add another number.
The following sample code adds all the numbers in the list, adds 50, and returns the sum:

  1. list = [200.40, 100, 400, 200.50, 400.40, 300.20]
  2. total = sum(list, 50)
  3. print("The total is", total)

Output:

The total is 1651.5

Example 4: Add all dictionary values

The sample code below adds all the dictionary values and returns the total:

  1. dictionary = {
  2. "item_001": 100,
  3. "item_002": 200,
  4. "item_003": 300,
  5. "item_004": 400
  6. }
  7. total = sum(dictionary.values())
  8. print("The total is", total)

Output:

The total is 1000

Example 5: Sum of two lists

This example shows how to sum multiple lists in Python.

The following example shows how to sum two lists of numbers:

  1. #sum two lists
  2.  
  3. list1 = [20, 10, 30, 40, 50]
  4. list2 = [60, 80, 70, 90, 100, 110]
  5. total = sum(list1) + sum(list2)
  6. print("The total is", total)

Example 6: Sum a list of lists

The following example shows a program that sums a list of lists:

  1. #sum a list of lists
  2.  
  3. total = 0
  4. list1 = [1, 2, 3, 4, 5]
  5. list2 = [6, 7, 8, 9, 10, 11]
  6. list3 = [12, 13, 14, 15, 16, 17]
  7. list =[list1, list2, list3]
  8. for l in list:
  9. total += sum(l)
  10. print("The total is", total)

Here, as you can see, we are adding all the numbers in the list using the for loop statement and the sum() function.

Summary

In this tutorial, you have learned how to use the Python sum() function, which adds up all the values in an iterable element such as a list, tuple, set, or dictionary and returns the sum.

You can use the sum() function to sum or add all elements in a list and sum a list of lists.


See also:
Python return Statement with Examples
Python List Methods and Functions with Examples
Python abs() – Absolute Value in Python
Python Factorial – Find the Factorial of a Number
Python min() – Find the Smallest Value in Python

Leave a Comment