Python | Finding Average of List

Python’s List Object type is quite useful for storing data while dealing with Scientific Computations where large amount of data need to be processed. Anyway, in this article => I’ll discuss How to Calculate average of numbers in a Python List?

Let’s see different Algorithmic approaches which can be implemented as Python Code calculating average of numbers in List. There are three algorithmic methods which can be used for this =>

  1. Using sum() Function for calculating average of numbers in List
  2. Using reduce() and lambda functions for calculating average of numbers in List
  3. Calculating average of numbers in a List using mean() function

Let’s discuss each one of these approaches by one-by-one.

Approach 1 => Find Average of List in Python

Below are algorithmic steps which can be followed for calculating average of numbers in a List using sum() function.

  1. Define Python List
  2. Define a Python Function Average(a_list) which divides sum of all elements in list with length of list using sum(a_list)/len(a_list) statement
  3. Just call the function Average(a_list)
# Python program to calculate the average of numbers in List

a_list = [15, 9, 55, 41, 35, 20, 62, 49]    # Define Python List

def Average(a_list):
	return sum(a_list) / len(a_list)

# Printing average of numbers in list rounded upto 2 digits
print("Average of the list => ", round(Average(a_list), 2))

Output of Above Code

Average of the list => 49.78

Approach 2 => Calculating Average of List in Python

Below are algorithmic steps which can be followed for calculating average of numbers in a List using reduce() and lambda functions.

  1. Import reduce function from functools Python Module
  2. Define a Python List as a_list
  3. Define a Python Function Average(a_list) which using reduce() function and len(a_list) calculates average of numbers in Python List.
# Python program to calculate average of a list Using reduce() and lambda

# importing reduce() function from functools module
from functools import reduce

a_list = [18, 38, 13, 675, 28]       # Define Python List

def Average(a_list):
	return reduce(lambda a, b: a + b, a_list) / len(a_list)

# Printing average of the list
print("Average of the list =>", round(Average(a_list), 2))

Output of Above Code

Average of the list => 154.4

Approach 3 => Figuring Out Average of Numbers in Python List

Below are algorithmic steps which can be followed for calculating average of numbers in a List using mean() function.

  1. Import mean function from Python’s Module statistics
  2. Define a Python List as a_list
  3. Define a Python Function Average(a_list) which takes in Python List and calculate its mean by passing list to mean() Function
  4. Calling function Average(a_list) passing in a list as parameter will return average of numbers in List
# Python program to get average of a list using mean() Function

# importing mean() function statistics Python Module
from statistics import mean

a_list = [72, 81, 98, 34, 17]            # Defining a Python List

def Average(a_list):
	return mean(a_list)

# Printing average of the list
print("Average of the list => ", round(Average(a_list), 2))

Output of Above Code

Average of the list =>  60.4

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts