How to Find the Average of a List in Python
The Python programming language has many built-in functions that simplify certain programming tasks. For example, finding the average of values stored in a list is made easy by the built-in functions sum() and len(). These allow you to find out the sum of all the values and the total number of items in a list. This is all you need to know to find the average value.
Things You'll Need
- Python 3.2 (see Resources)
Open the IDLE text editor in "Program Files" (or "Applications" for Macintosh), in the Python directory. A blank source code file opens.
Declare a list and fill it with values. One way is to write something like this:listValues = [ 32, 81, 43, 16, 72, 99, 14, 50]
Find the sum of all the values in the list using the sum() function. You can store the sum in a variable like this:listSum = sum(listValues)
Find the length of the list using the len() function. You can store the length in a variable like this:listLength = len(listValues)
Find the average value in the list by dividing the sum by the length, like this:listAverage = listSum / listLength
Print the average value using the print() function like this:print(listAverage)
Run the program by pressing the "F5" key. The program output looks like this:50.875