## What is the average of a list in Python

The average of a list can be calculated as the sum of all the items in a list, divided by the total number of items in the list. If there is only one item in a list form, then that item is its own average. However, if there are more than one items in the list, we must have the sum of all of them and then divided by n.

For example:

>>> a = [2 + 3, 4 * 9] >>> a.average() 4.0 >>> a = [1, 2, 2 * 4] >>> a.average() 12.0 >>> a = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’] >>> for item in a: … print item … a b c d e >>> a.average() 3.0

Note: The average of an empty list is 0, since there is no sum to divide by 0 items. This is different from many other languages, including C and PHP.

If you are in interested in more information on how to calculate the average of a list in Python, please visit http://www.python2tips.com/index/ average-list-python .

## How to find the average of a list in Python

In python, we can use the mean built-in function to find the average of a list. It is written as follows:

mean([1, 2, 3])

mean([2, 2.5])

mean([1.5, “one”, 1.6])

Please note that there are multiple ways to get the average in python: This article will show you the most common one. In all of the examples below we use the list created with three numbers: [1, 2, 3]. Each of these lists has an individual value and an individual count of items in it.

In this way:

list_count = len(list)

Value = list_count / list_count

But what if you want to get an average based on the value of all elements in the lists? This is where calculate_average() comes in. The calculate_average() function implements this idea: from math import sqrt mean = calculate_average([2, 3]) (1) subprocess.check_output(mean, “The output should be 2.5”)

This is an example code:

list_count = 0

while len(list) > 0 and list1 in list2:

if list_count % 2 == 1:

list_count += 1

## Examples of finding the average of a list in Python

If we want to write above mean function in Python3, then we can use the following code:

def mean(aList):

“””

Returns the average of a list of numbers

“””

total = 0.0

for x in aList:

total = total + x

return total/len(aList) or intp.x

total = 0.0

for x in aList:

total = total + x

return total/len(aList) or intp.x

You can also use the built-in filter to get the average of a list, e.g.:

def mean(aList):

“””Returns the sum of squares of the numbers in a list. Please note that it is a static method. If you need the average of a list, please use: def average(aList): “””

total = 0.0

for x in aList:

total = total + sqrt(x)

return total/len(aList)

You can also use the built-in summary to find the sum and sum of squares, e.g.:

def mean(aList):