The average or mean of a Python list, tuple, set

The average or mean of a Python list is calculated by statistics module.

import statistics

a = [1, 2, 3]

m = statistics.mean(a)

print(m)
# 2

This function works for Python list, tuple, set.

import statistics

a = (1.5, 2.1)
b = {3, 4}

print(type(a))  # <class 'tuple'>
print(type(b))  # <class 'set'>

m = statistics.mean(a)
n = statistics.mean(b)

print(m)
# 1.8

print(n)
# 3.5

Python set is a set of distinct values. So if you declare a set containing duplicate values, mean ignores them to calculate the average.

import statistics

a = {1, 1, 2}

m = statistics.mean(a)

print(m)
# 1.5

a looks like ${1, 1, 2}$ but is in fact only ${1,2}$.

Python Math

Python Tutorial