Sling Academy
Home/Python/Python: How to Calculate the Average of a Numeric List

Python: How to Calculate the Average of a Numeric List

Last updated: June 11, 2023

The main idea of calculating the average of a numeric list (a list that contains only numbers) can be described in two steps:

  1. Find the total sum of all elements of the list.
  2. Divide the total sum by the number of elements.

There are several ways to implement these steps with code. Let’s explore the best ones in this quick article.

Using the sum() and the len() functions

This is the most concise approach to get the job done. The sum() and the len() functions give you the total and the length of a list, respectively. A TypeError or ZeroDivisionError may be raised if the list contains non-numeric elements or is empty.

Example:

numbers = [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.1]

average = sum(numbers) / len(numbers)
print(average)

Output:

5.96

Performance: The sum() and len() functions have a time complexity of O(n), where n is the length of the list. They have a space complexity of O(1), as they do not create any new data structures.

Using a for loop

Instead of using convenient built-in functions like in the previous method, you can manually compute the average of a numeric list with a for loop.

Example:

numbers = [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.1]

# Initialize total
total = 0

# count the number of items in the list
count = 0

# Iterate over the list
for number in numbers:
    # Add each number to the total
    total += number
    # Increment the count
    count += 1

# Calculate the average
average = total / count
print(average)

Output:

5.96

In terms of performance, this approach is similar to the first one. However, when it comes to code conciseness and readability, it lags far behind.

Next Article: Using the list.insert() method in Python

Previous Article: Python: 4 Ways to Find the Sum of a Numeric List

Series: Python List Tutorials (Basic and Advanced)

Python

You May Also Like

  • Python Warning: Secure coding is not enabled for restorable state
  • Python TypeError: write() argument must be str, not bytes
  • 4 ways to install Python modules on Windows without admin rights
  • Python TypeError: object of type ‘NoneType’ has no len()
  • Python: How to access command-line arguments (3 approaches)
  • Understanding ‘Never’ type in Python 3.11+ (5 examples)
  • Python: 3 Ways to Retrieve City/Country from IP Address
  • Using Type Aliases in Python: A Practical Guide (with Examples)
  • Python: Defining distinct types using NewType class
  • Using Optional Type in Python (explained with examples)
  • Python: How to Override Methods in Classes
  • Python: Define Generic Types for Lists of Nested Dictionaries
  • Python: Defining type for a list that can contain both numbers and strings
  • Using TypeGuard in Python (Python 3.10+)
  • Python: Using ‘NoReturn’ type with functions
  • Type Casting in Python: The Ultimate Guide (with Examples)
  • Python: Using type hints with class methods and properties
  • Python: Typing a function with default parameters
  • Python: Typing a function that can return multiple types