In this post, you’ll learn how to use Python to find the average of a list, as well as of a list of list. You’ll learn how to do this using built-in methods like for-loops, the numpy library, and the statistics library.

The **Python Average** is calculated by adding up the items in a list and dividing it by the number of items in that list (which can be found using the length of that list).

Check out some other Python tutorials on datagy, including our complete guide to styling Pandas and our comprehensive overview of Pivot Tables in Pandas!

Table of Contents

## How can you calculate an average in Python?

Calculating the average of different values in Python is very straightforward. You simply add up an array of numbers and divide it by the length of that array.

One naive way that you can calculate the average in Python is using a for loop.

Let’s see how this can be done:

```
# Calculating the average of list using a for loop
numbers = [1,2,3,4,5,6,7,8,9]
sum = 0
count = 0
for number in numbers:
sum += number
count += 1
average = sum / count
print(average)
# Returns: 5.0
```

This is a fairly labour-intensive way of calculating an average in Python, but it shows how it can be done.

## Calculate Average of Python List using sum and len

Python doesn’t have a built-in function to calculate an average of a list, but you can use the `sum()`

and `len()`

functions to calculate an average of a list.

In order to do this, you first calculate the sum of a list and then divide it by the length of that list. Let’s see how we can accomplish this:

```
# Calculating the average of a list using sum() and len()
numbers = [1,2,3,4,5,6,7,8,9]
average = sum(numbers) / len(numbers)
# Returns 5.0
```

If you find yourself doing this frequently in a script, you can turn this into a function. That way you can pass in any list and return its average. let’s see how to do this:

```
# Creating a function to calculate an average of a list
numbers = [1,2,3,4,5,6,7,8,9]
def average_of_list(list_to_use):
return sum(list_to_use) / len(list_to_use)
print(average_of_list(numbers))
# Returns: 5.0
```

In the example above, you turned the simple calculation of dividing the sum of a list by its length into a function to make it re-usable throughout your scripts.

## Calculate Average of Python List using Numpy

If you’re working with numbers, chances are you’re using `numpy`

in your script, meaning you can easily use some of its built-in functions. Numpy comes built-in with a number of mathematical functions that make it easy to calculate various statistics, such as an average.

Let’s see how we can work with numpy to calculate the average of a list:

```
# Using numpy to calculate the average of a list
from numpy import mean
numbers = [1,2,3,4,5,6,7,8,9]
average = mean(numbers)
print(average)
# Returns: 5.0
```

Let’s see what we’ve done here:

- We imported
`mean`

from numpy, which is a function used to calculate the mean - We then load our list
`numbers`

- We then create a new variable and use the
`mean`

function, passing in the list as the argument

Next, let’s see how we can calculate the average of a Python list using the statistics library.

## Find Average of List Using the Statistics Library

The statistics library is a Python library that lets you calculate, well, statistics. Similar to numpy, the statistics library has a `mean`

function built into it. Let’s see how we can use this function to calculate the mean of a Python list:

```
# Using the statistics library to calculate a mean of a list
from statistics import mean
numbers = [1,2,3,4,5,6,7,8,9]
average = mean(numbers)
print(average)
# Returns: 5.0
```

Let’s see what we accomplished here:

- We imported the
`mean()`

function from the statistics library - We loaded in our list of numbers
- We passed this list in as an argument to the
`mean()`

function

## Find Average of Python List of Lists

There may be many times when you want to calculate the average of a list of lists.

But first: what is a list of lists? A list of list is, simply, a list that contains other lists.

Let’s say you’re given a list of list like below:

```
list_of_lists = [
[1,2,3],
[4,5,6],
[7,8,9]
]
```

And you wanted to turn it into this:

```
row_averages = [2.0, 5.0, 8.0]
column_averages = [4.0, 5.0, 6.0]
```

### Use Zip to Calculate the Average of a List of Lists

We can use the Python `zip()`

function to calculate the average of a list of lists. To learn more about the `zip()`

function, check you my tutorial here. We can use the `zip()`

function in conjunction with list comprehensions, which you can learn about in my video here:

Let’s see how we can accomplish this:

```
# Calculating an average of a list of lists using zip()
list_of_lists = [
[1,2,3],
[4,5,6],
[7,8,9]
]
column_average = [sum(sub_list) / len(sub_list) for sub_list in zip(*list_of_lists)]
row_average = [sum(sub_list) / len(sub_list) for sub_list in list_of_lists]
print(column_average)
print(row_average)
# Returns:
# [4.0, 5.0, 6.0]
# [2.0, 5.0, 8.0]
```

Let’s see what we’ve done here:

- We loaded a list of lists and assigned it to
`list_of_lists`

- We then calculated by a row-wise average and a column-wise average
- The column-wise average uses the
`zip()`

function to access to ith item in each list - The row-wise average calculates the average of each sublist

### Use Numpy to Calculate the Average of a List of Lists

Similar to above, we can use `numpy`

to calculate the average of a list of lists. In order to calculate a row-wise and a column-wise mean of a list of lists, we can simply modify the `axis=`

parameter.

The numpy implementation is a bit easier to read, though it does involve importing an additional package, if you’re not already using it:

```
list_of_lists = [
[1,2,3],
[4,5,6],
[7,8,9]
]
from numpy import array, average
array_lol = array(list_of_lists)
column_average = average(array_lol, axis=0)
row_average = average(array_lol, axis=1)
print(column_average)
print(row_average)
# Returns
# [2. 5. 8.]
# [4. 5. 6.]
```

We can see that this is a bit easier to write and to read. We use the `axis=0`

to calculate across columns, and `axis=1`

to calculate across rows.

## Conclusion

In this post, you learned how to calculate the Python average of a list and of lists of lists. You learned how to do this with built-in functions including sum and len, as well as external packages including `numpy`

and `statistics`

. Finally, you learned how to calculate both row-wise and column-wise averages of lists of lists.

To learn more about the numpy average function, check out the official documentation here.