How to Get the Average of a List in Python
This tutorial explains how to calculate the average of a list in Python. It also lists some sample codes to further clarify the concepts as the method has changed from previous Python versions.
statistics
Get the average of a list
using the library
If you are using Python 3.4+, you can use the newly introduced statistics
library. This library contains multiple mathematical functions that can be used right out of the box. Since we want to calculate the average of a given list, we will use statistics.mean(list)
. list
is a list of numbers. This function will return the average (mean) of the given list.
A basic example using this statistics.mean()
method is shown below.
import statistics
list = [1, 2, 3, 4, 5, 6]
mean = statistics.mean(list)
print(mean)
Output:
3.5
sum()/len()
Finding the average of a Python list
using
Using statistics
the library to calculate the average of a list is not the only option. The average of a list can be calculated by simply dividing the sum of the elements by the number of elements.
sum(list)
Gets the sum of the given list and len(list)
returns the length of the list.
data = [1, 2, 3, 4, 5, 6]
mean = sum(data) / len(data)
print(mean)
Output:
3.5
numpy.mean()
Finding the average of a Python list
using
We can also numpy.mean()
get the average of a list in Python using the average function. The average is taken over the flattened array by default, otherwise it is taken over the specified axis.
However, before using it, you need to install NumPy
the module.
The following is a sample code for using numpy.mean()
to get the average of a list.
import numpy
data = [1, 2, 3, 4, 5, 6]
mean = numpy.mean(data)
print(mean)
Output:
3.5
sum()/float(len())
Find the Average of a Python List
in Python 2
If your Python version is 2.x, then you cannot use statistics
the module and must use simple math to calculate the average of a given list.
For Python 2, you need to len
convert to a floating point number to get a floating point result. The code will look like this:
data = [1, 2, 3, 4, 5, 6]
mean = sum(data) / float(len(data))
print(mean)
Output:
3.5
If you don't len
convert to float
, you won't get a floating point number but an integer, as shown below.
data = [1, 2, 3, 4, 5, 6]
mean = sum(data) / len(data)
print(mean)
Output:
3
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Finding a string in a list in Python
Publish Date:2025/05/09 Views:75 Category:Python
-
This tutorial shows you how to find elements from a Python list that have a specific substring in them. We will use the following list and extract ack the strings that have in it. my_list = [ "Jack" , "Mack" , "Jay" , "Mark" ] for Find elem
Getting list shape in Python
Publish Date:2025/05/09 Views:139 Category:Python
-
In Python, knowing the shape of a list is very important for working with data structures, especially when it comes to multidimensional or nested lists. This article explores various ways to determine the shape of a list in Python, from sim
Adding multiple elements to a list in Python
Publish Date:2025/05/09 Views:180 Category:Python
-
List is a mutable data structure in Python. It can contain values of different types. This article will discuss some methods to append single or multiple elements to a Python list. append() Append a single element in a Python list usi
Get the index of the maximum and minimum values in a list in Python
Publish Date:2025/05/09 Views:117 Category:Python
-
In this tutorial, we will discuss methods to get the index of the maximum and minimum value of a list in Python. max() Get the index of the maximum value in a list using and list.index() functions in Python max() The function gives the maxi
List of numbers from 1 to N in Python
Publish Date:2025/05/09 Views:116 Category:Python
-
This tutorial will discuss how to create a list of numbers from 1 to some specified number. Create a user-defined function to create a list of numbers from 1 to N This method will take the desired number from the user and for iterate until
Convert List to Pandas DataFrame in Python
Publish Date:2025/05/09 Views:133 Category:Python
-
This article will show you how to convert items in a list into a Pandas DataFrame. Convert List to Pandas DataFrame in Python DataFrame, in general, is a two-dimensional labeled data structure. Pandas is an open source Python package that i
Sorting a list by another list in Python
Publish Date:2025/05/09 Views:148 Category:Python
-
Normally, when we sort a list, we do it in ascending or descending order. However, we can sort a list based on the order of another list in Python. We will learn how to sort a given list based on the values in another list in this art
Normalizing a list of numbers in Python
Publish Date:2025/05/09 Views:134 Category:Python
-
Normalization means converting the given data to another scale. We rescale the data so that it is between two values. Most of the time, the data is rescaled between 0 and 1. We rescale data for different purposes. For example, machine learn
How to create a list of a specific size in Python
Publish Date:2025/05/09 Views:195 Category:Python
-
Preallocating storage for a list or array is a common practice among programmers when they know the number of elements in advance. Unlike C++ and Java, in Python you must initialize all preallocated storage with some value. Typically, devel