How to Find Mean of a List Python

In this Python tutorial, you will learn how to find the mean of a list Python.

In data analysis, wherever I need to find trends in the data based on the mean value, I use the Python mean() function from the statistics module.

I have also explained some other methods for finding the mean of a list in Python. After completing this tutorial, you will be able to compute the mean of any list easily.

So, let’s start,

The Mean of a List Python

First, to find the mean of a list in Python, I want to explain what means are and how to calculate them. Remember that means are also known as averages.

The average is the sum of elements divided by the number of elements. For example, you have a list of numbers like this [3, 5, 7]. As you can see, this list contains 3 numbers or elements. Find the average or mean of this list of numbers.

First, sum all the numbers like this: 3+5+7, which becomes equal to 15; now divide the 15 by the number of elements in the list. As you can the list contains 3 elements, so 15/3, which yields 5, so the mean of the list is 5.

So, here, you will understand the different approaches to finding the mean or average of the list in Python.

Python Mean of List using sum() and len() Function

Python has a function sum(), which returns the sum of the elements or numbers, whereas the len() function takes the list and returns the length of the list or the number of elements in the list.

READ:  Python Tkinter Image + Examples

Let’s use these functions together to find the mean of the list in Python.

Suppose you have a list of numbers as shown below.

numbers = [22, 30, 80, 90, 150]

Now, use the sum() function to compute the sum of elements and the len() function to know the number of elements in the list and then compute the mean as shown below.

mean = sum(numbers) / len(numbers)
print(f'The mean is: {mean}')
Python Mean of List using sum() and len() Function

Look at the output. The mean of the list numbers is 74.4. Here, the code part mean = sum(numbers) / len(numbers) computes the sum of elements using the sum(numbers) and the number of elements or numbers in the list using the len(numbers).

Then it divides the sum(numbers) by len(numbers) to find the mean of the list. This is how to find the mean of a list in Python.

Mean List Python using Statistics Module

Python has a module called statistics, which contains a function known as mean. This function allows you to compute the mean of the given numbers.

So to use the function mean(), first import it from the module statistics as shown below.

from statistics import mean

Again you have the same list as shown below.

numbers = [22, 30, 80, 90, 150]

Now, pass the list of numbers to the mean() function as shown in the code below.

mean_of_list = mean(numbers)
print(f'The mean is: {mean_of_list}')
Mean List Python using Statistics Module

The output shows that the statistics module’s mean() function returns the list’s average as 74.4.

Pass any list to the mean() function to compute its mean. This is how to get the mean of a list in Python.

READ:  Pandas Replace Multiple Values in Column based on Condition in Python [4 Methods]

The Mean of a List using For Loop

You can use the for loop to compute the list’s mean by iterating over each number in the list, adding them together, and then dividing by the list’s length.

For example, you need to find the average sales revenue per day or month to identify trends. if a store’s daily revenue for the past week is stored as the list shown below.

[200, 300, 250, 400, 350, 320, 280]

Add the sales revenue for each day using the code below.

total_revenue = 0
for rev in revenue:
    total_revenue += rev

Now compute the average sales revenue using the code below.

average_revenue = total_revenue / len(numbers)
print(f'The mean is: {average_revenue}')
The Mean of a List using For Loop

From the output, you can see the average sales revenue is 420.0 of the list ‘revenue’.

Let’s understand the code part where total_revenue is initiated with 0, and the variable contains the sum of all the revenue in the list.

Then the loop ‘for rev in revenue:’ accesses each revenue from the list in each iteration and adds that revenue to the total_revenue using the total_revenue += rev.

After that, this line of code ‘average_revenue = total_revenue / len(numbers)’ divides the total_revenue by the number of elements in the list using len(numbers) and the mean is stored in the new variable called average_revenue.

As a result, you get the mean of the list ‘revenue’ as 420.0; this is how to find the man of the list using the for loop in Python.

From the above different approaches, I hope that you understand how to find the mean of any list.

READ:  np.diff() function in Python [5 Examples]

Conclusion

In this Python tutorial, you learned how to find the mean of a list in Python using the sum() and len() functions together, you also used the for loop and statistics module to find the mean of the list in Python.

You may like to read: