Python 3.6+statisticsfunction
statistics.harmonic_mean
mean
Harmonic mean (reciprocal of the mean of reciprocals).
Syntax
statistics.harmonic_mean(data)
It fits averaging rates over equal amounts of work: half the distance at 60 and half at 30 averages 40, not 45. A single zero makes the whole result zero — the algorithm short-circuits there and never validates the remaining values — while a negative value raises StatisticsError. The weights argument arrived in 3.10.
Examples
# Python 3.10+
import statistics
print(statistics.harmonic_mean([1, 2, 4])) # → 1.7142857142857142
print(statistics.harmonic_mean([10, 40])) # → 16.0
print(statistics.mean([60, 30]), statistics.harmonic_mean([60, 30])) # → 45 40.0
print(statistics.harmonic_mean([40, 60], weights=[5, 30])) # → 56.0
print(statistics.harmonic_mean([1, -2])) # → StatisticsError
See also
Open in the full glossarydocs.python.orgReport an inaccuracy