Unfortunately, Python does not provide a standard built-in function to calculate the average of a list of numbers. Finding the average of a list of numbers is not that hard. All we need to do is divide the total sum of elements by the total number of elements present in the list.
To calculate the sum of the list, we can use the Python
sum()
function, and to find the total number of elements, we can use the
len()
function.
In this tutorial, we will walk through two different techniques to compute the average of a list of numbers. And to have a better understanding of the techniques we will discuss the examples. So let's get started.
Python Average Overview
There are two most common ways to calculate the average of a list of numbers in Python.
- By dividing the total sum of the list by the total number of elements present in the list.
-
By using the Python
statistics.mean()
method.
There are many third-party Python mathematical computational libraries like numpy, scipy, and pandas that also provide the methods to calculate the average of a list or array. But here we will not be discussing those because, in order to use those libraries, we need to install them, and for such trivial tasks as finding the average of a list, we should not depend on those powerful libraries.
1. Python Average: Using sum() and len() functions
The most common and straightforward way to calculate the average of a Python list is by finding the sum of the list using
sum()
function and divide that sum by the total number of elements, which can find out using
len()
function.
sum()
:
sum
is a Python standard function that accepts a list and a tuple of numbers as an argument value and returns the total sum of the numbers present in the list or tuple.
len()
:
len
is also a Python standard function, which can accept an iterable object as an argument value and return the total number of elements or characters present in that iterable object.
Syntax
The syntax to calculate average using sum() and
len()
functions.
average = sum(list_name) / len(list_name)
Example
Let's say you have 3 bill lists
june_bills
,
july_bills
, and
august_bills
, and all these thee lists contains the amount of money you spend on different products in the month of June, July, and August. And now you need to find out the average spend every month.
june_bills = [23, 3443.33, 764.48, 8736, 848, 342, 100] july_bills = [33,536.37, 223.34, 8364,823, 263, 947] august_bills = [984, 764, 49, 678, 3647,234, 113] # cumpute the average for three months june_average = sum(june_bills)/ len(june_bills) july_average = sum(july_bills)/ len(july_bills) august_average = sum(august_bills)/len(august_bills) print("Average June Spending: ", round(june_average, 2)) print("Average July Spending: ", round(july_average,2)) print("Average August Spending: ", round(august_average,2))
Output:
Average June Spending: 2036.69 Average July Spending: 1598.53 Average August Spending: 924.14
2. Python Average: Using statistics.mean()
Python provides an in-built module
statistics
, and this module comes with many functions to calculate mathematical statistics on numerical data. Among all the functions provided by statistics modules, the
mean()
method can be used to calculate the arithmetic mean or average of an iterable object.
Syntax
import statistics average = statistics.mean(list_name)
Example
Let's implement the same example as we discussed in the
sum()
, and
len()
section. But now, instead of
sum()
and
len()
, we will use the
mean()
method to calculate the average sum.
import statistics june_bills = [23, 3443.33, 764.48, 8736, 848, 342, 100] july_bills = [33,536.37, 223.34, 8364,823, 263, 947] august_bills = [984, 764, 49, 678, 3647,234, 113] # cumpute the average for three months using mean() method june_average = statistics.mean(june_bills) july_average = statistics.mean(july_bills) august_average = statistics.mean(august_bills) print("Average June Spending: ", round(june_average, 2)) print("Average July Spending: ", round(july_average,2)) print("Average August Spending: ", round(august_average,2))
Output
Average June Spending: 2036.69 Average July Spending: 1598.53 Average August Spending: 924.14
Wrapping Up!
Finding the average of a list of numbers is very easy. We can either use
sum()
&
len()
technique or statistics.mean() method to compute the average of a list or tuple elements. If you ever encounter this problem in your program, there you must use the
sum()
and
len()
technique because importing a module just for one function is not a great idea.
You can also follow a long approach where you first calculate the total sum of a list using for loop and then divide the total sum by the total number of elements. But it would not be a Pythonic way, if you have an inbuilt function to perform tasks, you should always use that one.
People are also reading:
- Python Compare Strings
- Replace Item in Python List
- Python Counter in Collections
- COPY File and Directory Using shutil in Python
- Assembly, Disassembly and Emulation using Python
- Detect Contours in Images in Python
- Python Developer Salary
- 10 Top Essential Python Tricks & Tips
- Python Matrix
- Arrays in Python
- Python Interview Questions
Leave a Comment on this Post