All IT Courses 50% Off
Python Tutorials

Finding the Average of a List in Python

4 Different Methods

The average of data is calculated as the ratio of the sum of numbers to the count of numbers. It is an important statistical description of any data. The average (otherwise called the mean) is a commonly used metric, used to show the central tendency of the data. If you will be working as a data scientist, you need to know how to calculate the average of a list. And that’s what we would be discussing in this tutorial.

By the end of this tutorial, you will learn how to calculate the mean of a list using 4 different methods. 

Specifically, here’s what you’d learn.

  • How to find average using for loop
  • How to find average using sum and count methods
  • How to find average using the mean method in the NumPy library

Let’s get on with it.

Finding the average using Loop

You can calculate the average of numbers in a list by using a for loop. You begin by creating the list you wish to find its average then create a variable to store the sum of numbers. The variable will be set to 0.  Afterward, loop over every element in the list and add to the sum variable. Finally, divide the sum by the len of the list. An example is shown below.

All IT Courses 50% Off
#receive list from user
list_to_add = input('Input a list of numbers separated by comma then space: ')
try:
    #split the user input by spaces
    list_to_add = list_to_add.split(', ')
    list_sum = 0
 
    #loop over the list and add number to sum
    for number in list_to_add:
        list_sum = int(number) + list_sum
 
    #calculate the average
    list_avg = list_sum / len(list_to_add)
    print('The average of the numbers you entered is: ', list_avg)
except ValueError:
    print('Please enter a list of number separated by a comma and space only')
Output:
Input a list of numbers separated by a comma then space: 12, 34, 6, 43, 22
The average of the numbers you entered is:  23.4

Using the sum and len inbuilt functions

You can calculate the sum of numbers in a list by calling the inbuilt sum function, rather than looping the list and adding the sum to an initialized variable. This way, the code is more compact and shorter. The code is shown below. 

#define some list of numbers 
list_to_add = [12, 12, 4, 5, 7, 23, 65, 12, 54]
    
#calculate the average
list_avg = sum(list_to_add) / len(list_to_add)
 
print('The average of the numbers in the list is: ', list_avg)
Output: The average of the numbers in the list is:  21.555555555555557

Using the statistics.mean() method

Another faster way of calculating the average of a list is by exploring the mean function from the statistics module. Once the function is called and the list is passed as an argument, it does the average calculation and returns the result. See an example below. 

#import the necessary library
import statistics
 
# define some list of numbers 
list_to_add = [12, 12, 4, 5, 7, 23, 65, 12, 54]
    
#calculate the average
list_avg = statistics.mean(list_to_add)
 
print('The average of the numbers in the list is: ', list_avg)
Output:
The average of the numbers in the list is:  21.555555555555557

Using the numpy.mean method

Numpy has a method called the mean() method which is used to calculate the average of a list. It is like the previous method just that this time, the method is imported from the NumPy library. See an example below.

#import the necessary library
import numpy
 
# define some list of numbers 
list_to_add = [12, 12, 4, 5, 7, 23, 65, 12, 54]
    
#calculate the average
list_avg = numpy.mean(list_to_add)
 
print('The average of the numbers in the list is: ', list_avg)
Output:
The average of the numbers in the list is:  21.555555555555557

To conclude, you have seen the four different methods of finding the average of numbers in a list. 

  • You could either loop over the list and calculate the mean manually or calculate the average
  • You could use the sum and len function and find the ratio
  • You could use the mean method from the statistics module
  • You could use the mean method from the Numpy module. 

If you have any questions, feel free to leave them in the comment section and I’d do my best to answer them.

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button