How to Find or Calculate Harmonic Mean in Python using harmonic_mean() Function

harmonic_mean() function is from Standard statistics library of Python Programming Language. The basic purpose of this function is to calculate the simple Harmonic Mean of given data. The given data will always be in the form of sequence or iterator.

The Harmonic Mean is sometimes called Subcontrary Mean; the Harmonic Mean is also representing the central location of data. It is mostly used when given quantities are rates and ratios.

Following is the statistical formula of the Harmonic Mean.

Harmonic Mean Formula:
Harmonic Mean Formula

x1, x2, x3… must be greater than Zero.
As you can clearly see in the formula that Harmonic Mean is the reciprocal of the Arithmetic Mean of the reciprocals of the given data.


Syntax of harmonic_mean() Function in Python

The syntax of harmonic_mean() function in Python is as follows:



  • This function is new in Python 3.6.
  • Harmonic Mean doesn’t support negative values in list, tuple or any iterator.


Parameters of harmonic_mean() Function in Python

data – This parameter can be any sequence or iterator (list, tuple)Note: If data in iterator is empty, harmonic_mean() function raise a StatisticsError


Return Value of harmonic_mean() in Python

Return value of harmonic_mean() is a floating point number after calculating the Harmonic Mean of given data in a sequence or iterator (lists, tuples)


harmonic_mean() Function Examples in Python

# import statistics library
import statistics


data = list([1,2,3,4,5,6,7,8,9])

Output of harmonic_mean() Function in Python