# Python Program to Calculate the Average of Numbers in a Given List

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

List in Python :

The list data type is one of the most often used data types in Python. The square brackets [ ] easily identify a Python List. Lists are used to store data items, with each item separated by a comma (,). A Python List can include data elements of any data type, including integers and Booleans.

One of the primary reasons that lists are so popular is that they are mutable. Any data item in a List can be replaced by any other data item if it is mutable. This distinguishes Lists from Tuples, which are likewise used to store data elements but are immutable.

Given a list, the task is to Calculate the average of all the numbers in the given list in python.

Examples:

Example1:

Input:

given list = [8, 9, 1, 23, 15, 20, 19, 13, 8, 7, 5, 2, 7, 10, 14, 16]

Output:

The average value of the given list [8, 9, 1, 23, 15, 20, 19, 13, 8, 7, 5, 2, 7, 10, 14, 16] = 11.0625

Example2:

Input:

given list = [  47 24.5 27 28 11 23 34.8 33 31 29 45 37 39 ]

Output:

The average value of the given list [47.0, 24.5, 27.0, 28.0, 11.0, 23.0, 34.8, 933.0, 31.0, 29.0, 45.0, 37.0, 39.0] = 100.71538461538461

## Program to Calculate the Average of Numbers in a Given List in Python

There are several ways to calculate the average of all numbers in the given list in Python some of them are:

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

### Method #1: Counting sum manually ( Static Input separated by spaces)

Approach:

• Give the list input as static
• Take a variable say sumOfList which stores the sum of all list elements and initialize it to 0.
• Traverse the list using for loop
• For each iteration add the  iterator value to the sumOfList.
• Calculate the length of list using len() function.
• Calculate the average of given list by Dividing sumOfList with length.
• Print the Average

Below is the implementation:

# given list
given_list = [8, 9, 1, 23, 15, 20, 19, 13, 8, 7, 5, 2, 7, 10, 14, 16]
# Take a variable say sumOfList which stores the
# sum of all list elements and initialize it to 0.
sumOfList = 0
# Traverse the given list using for loop
for eleme in given_list:
# Add the iterator value to the sumOfList after each iteration.
sumOfList = sumOfList+eleme
# calculating the length of given list using len() function
length = len(given_list)
# Calculate the average value of the given list by Dividing sumOfList with length
listAvg = sumOfList/length
# printng the  the average value of the given list
print("The average value of the given list", given_list, "=", listAvg)


Output:

The average value of the given list [8, 9, 1, 23, 15, 20, 19, 13, 8, 7, 5, 2, 7, 10, 14, 16] = 11.0625

### Method #2:Using sum() function ( Static Input separated by spaces)

Approach:

• Give the list input as static
• Calculate the sum of list using sum() function and store it in variable sumOfList.
• Calculate the length of list using len() function.
• Calculate the average of given list by Dividing sumOfList with length.
• Print the Average

Below is the implementation:

# given list
given_list = [8, 9, 1, 23, 15, 20, 19, 13, 8, 7, 5, 2, 7, 10, 14, 16]
# Calculating the sum of list using sum() function and store it in variable sumOfList.
sumOfList = sum(given_list)

# calculating the length of given list using len() function
length = len(given_list)
# Calculate the average value of the given list by Dividing sumOfList with length
listAvg = sumOfList/length
# printng the  the average value of the given list
print("The average value of the given list", given_list, "=", listAvg)


Output:

The average value of the given list [8, 9, 1, 23, 15, 20, 19, 13, 8, 7, 5, 2, 7, 10, 14, 16] = 11.0625

### Method #3:Using sum() function ( User Input separated by spaces)

Approach:

• Scan the list separated by spaces using map, list(),split() and float functions (Because list values can be decimal also).
• Calculate the sum of list using sum() function and store it in variable sumOfList.
• Calculate the length of list using len() function.
• Calculate the average of given list by Dividing sumOfList with length.
• Print the Average

Below is the implementation:

# Scan the list separated by spaces using map and float functions (Because list values can be decimal also)
given_list =list(map(float,input("Enter some random elements of the list : ").split()))
# Calculating the sum of list using sum() function and store it in variable sumOfList.
sumOfList = sum(given_list)

# calculating the length of given list using len() function
length = len(given_list)
# Calculate the average value of the given list by Dividing sumOfList with length
listAvg = sumOfList/length
# printng the  the average value of the given list
print("The average value of the given list", given_list, "=", listAvg)


Output:

Enter some random elements of the list : 47 24.5 27 28 11 23 34.8 33 31 29 45 37 39
The average value of the given list [47.0, 24.5, 27.0, 28.0, 11.0, 23.0, 34.8, 933.0, 31.0, 29.0, 45.0, 37.0, 39.0] = 100.71538461538461

Explanation :

We used the split function to split the input string separated by spaces, then used the map function to convert all of the strings to floats and placed all of the items in a list.

Related Articles: