How to Find Average of A List in Python
In this article, we will learn to find the average of a list of elements using Python script. We will use some built-in functions and some custom codes as well. Let's first have a quick look over what is a list and then how we find the average of the list in Python.
List
The list is one of Python's built-in Data types. It is a data type where data can be stored in a different form. The list uses square brackets with comma-separated values to store data. Data inside the lists can be of any type say, integer, string, or float value. String data should be in double quotes or single quotes. Lists can be defined using any variable name and then assigning different values to the list in a square bracket.
List Example
list1 = [‘Ram’,’Arun’,‘Kiran’]
list2 = [16,78,32,67]
list3 = [‘apple’,’mango’,16,’cherry’,3.4]
The list is in the order form which means during the print statement order of the original list will not be changed. The list is a mutable data type which means values can be changed upon different operations. Lists also allow duplicate values inside the square brackets.
Find Average of a List in Python
To find the average of a list, a list should be defined with a numerical set of values inside square brackets. The average of lists can only be calculated on numerical values, not on string values. The average is the sum of elements divided by the number of elements. Let's see some examples.
Example: Find Average Using sum() and len() Function
In Python, we can find the average of a list by simply using the sum()
and len()
function. In this example, the sum() and len() built-in functions are used. It is a straightforward way to calculate the average as no loop is required to run through the elements, and also, the code size is reduced. The average can be calculated with just one line of code as shown below.
list1 = [15, 9, 5, 4, 3]
average = sum(list1) / len(list1)
# Printing average of the list
print("Average of the list =", average)
Average of the list = 7.2
Example: Find Average Using sum() and len() Function
We can use the inbuilt reduce()
function to reduce the loop and by using the lambda function, we can compute the summation of the list. We use len()
function to calculate length. For using the reduce function, reduce has to be imported from functools
module. See the below example.
# importing reduce()
from functools import reduce
def Average(lst):
return reduce(lambda a, b: a + b, lst) / len(lst)
# Driver Code
lst = [15, 9, 55, 41, 35, 20, 62, 49]
average = Average(lst)
# Printing average of the list
print("Average of the list =", average)
Average of the list = 35.75
Example: Find Average Using mean() Function
The inbuilt function mean()
can be used to calculate the mean( average ) of the list. You can easily calculate it by importing the statistics
module into our Python script.
# importing mean()
from statistics import mean
def Average(lst):
return mean(lst)
# Driver Code
lst = [15, 9, 55, 41, 35, 20, 62, 49]
average = Average(lst)
# Printing average of the list
print("Average of the list =", average)
Average of the list = 35.75
Example: Find Average Using for Loop
In this example, we have initialized the variable sum_num to zero and used for loop. The for-loop will loop through the elements present in the list, and each number is added and saved inside the sum_num variable. The average is calculated by using the sum_num divided by the count of the numbers in the list by using the len() built-in function.
def average(num):
sum_num = 0
for x in num:
sum_num = sum_num + x
avg = sum_num / len(num)
return avg
l = [18,25,3,41,5]
print("The average is", average(l))
The average is 18.4
Example: Find Average Using mean() function of NumPy
Numpy library is a commonly used python library to work on large multi-dimensional arrays. It additionally has a huge assortment of numerical functions to be utilized on large arrays to perform different tasks. Numpy uses mean()
function to calculate the average of the list. For this, the mean is imported from the NumPy module.
from numpy import mean
list1 = [18, 25, 3, 41, 5]
average = mean(list1)
print("The average is ", average)
The average is 18.4
Conclusion
So, the python list is a data type on which multiple functions can be applied. There are 5 different ways to find an average of a list
-
Using the inbuilt function sum ()
-
Using reduce() and lambda()
-
Using inbuilt average function - mean()
-
Average via for Loop
-
Using mean() from the
NumPy library