| by Arround The Web | No comments

Python Average of List

In mathematics, an average is a calculation of the central tendency of a set of numbers/values. It is a way to represent the generic value of a set of numbers. There are a few types of averages: “mean”, “median”, and “mode”. In this Python blog, we will focus on the “mean” average. It is such that we will explore calculating the Python average of a list and provide step-by-step instructions on how to do it using various methods.

What is the Mean Average?

The “mean average” is the sum of every number/value in a set divided by the total count of numbers in the set. Alternatively, it is also called the “arithmetic” mean. The mean is the most familiar type of average and is often used in statistics and data analysis.

How to Calculate/Get the Average of a Python List?

To calculate the Python list average, various methods are used in Python. Some of the methods are listed below:

Method 1: Get the Average of the List Utilizing the “sum()” and “len()” Functions

The following steps are employed to calculate/get the list average by utilizing the “sum()” and “len()” functions.

Step 1: Create a List

The first step is to make/create a list of numbers. A list can be constructed by utilizing square brackets and separating the elements/item values with commas. Here is an example:

my_list = [45, 42, 23, 14, 55]

 

Step 2: Calculate the Sum of the List

Next, we need to calculate the list sum by utilizing the built-in function “sum()”. Here is an example:

list_sum = sum(my_list)

 

Step 3: Calculate the Length of the List

The third step is to calculate/get the list length by utilizing the built-in Python function “len()”. Following is an example:

list_length = len(my_list)

 

Step 4: Calculate the Average

Finally, we can calculate the average by dividing the sum of the list by the length of the list, as follows:

list_average = list_sum / list_length

 

Step 5: Putting it all Together

Here’s the entire code to calculate/get the Python list average:

my_list = [45, 42, 23, 14, 55]
list_sum = sum(my_list)
list_length = len(my_list)
list_average = list_sum / list_length
print("The average of the list is:", list_average)

 

In the above code, the average of the list is calculated using the “sum()” and “len()” functions, and the “division” operator, respectively.

Output

The above snippet shows the average of the list.

Method 2: Get the Average of the List Using the “numpy” Library

The “numpy” library is utilized for scientific computing and it provides many useful functions, including the ability to get the list average. This library can also be utilized to compute the list average.

Example

The “numpy.mean()” function is utilized to compute the list average in the below code:

import numpy
my_list = [45, 42, 23, 14, 55]
list_average = numpy.mean(my_list)
print("The average of the list is:", list_average)

 

According to the above code:

  • As a foremost step, we need to import the “numpy” library.
  • The second step is finding the average of the created list which is carried out by the numpy function “mean()”.

Output

In the above snippet, you can see that the average of the list is retrieved appropriately.

Method 3: Get the Average of the List Using the “statistics” Module

The “statistics” module presents the “mean()” function that is also used to get the list average. The specified module was introduced/presented in Python 3.4 and provides numerical stability with floats.

Example

This code block is utilized to get the list average:

from statistics import mean
my_list = [88, 77, 99, 65, 32]
list_average = mean(my_list)
print("The average of the list is:",list_average)

 

In the above code block, the “mean()” function of the “statistics” module is utilized to estimate the list average.

Output

Method 4: Get the Average of the List Utilizing the “for” Loop

The “for” loop is also utilized to apply the desired computation by iterating over the list of elements and adding them to a variable that stores the sum. After that, divide the total by the list length to get/calculate the average.

Example

The below block of code is used to calculate the list average:

my_list = [45, 42, 23, 14, 55]
sum_of_list = 0
for x in my_list:
    sum_of_list += x
list_average = sum_of_list / len(my_list)
print("The average of the list is:", list_average)

 

Here, the “for” loop is used to iterate over the list and update the value of “sum_of_list” by adding each list element to it. After that, perform the division by the list length to return the list average.

Output

The average of the list has been calculated in the above snippet accordingly.

Conclusion

The “sum()” and “len()” functions, the “numpy” library, the “statistics” module, or the “for” loop is used to get the list average in Python. The desired functionality can be achieved via the built-in Java functions or the libraries. This write-up demonstrated the computation of the Python list average.

Share Button

Source: linuxhint.com

Leave a Reply