Open In App

Python | Harmonic Mean of List

Improve
Improve
Like Article
Like
Save
Share
Report

While working with Python, we can have a problem in which we need to find harmonic mean of a list cumulative. This problem is common in Data Science domain. Let’s discuss certain ways in which this problem can be solved. 

Method #1 : Using loop + formula The simpler manner to approach this problem is to employ the formula for finding harmonic mean and perform using loop shorthands. This is the most basic approach to solve this problem. 

Python3




# Python3 code to demonstrate working of
# Harmonic Mean of List
# using loop + formula
 
# initialize list
test_list = [6, 7, 3, 9, 10, 15]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Harmonic Mean of List
# using loop + formula
sum = 0
for ele in test_list:
    sum += 1 / ele   
res = len(test_list)/sum
 
# printing result
print("The harmonic mean of list is : " + str(res))


Output : 

The original list is : [6, 7, 3, 9, 10, 15]
The harmonic mean of list is : 6.517241379310345

Time Complexity: O(n) where n is the number of elements in the string list. The loop + formula is used to perform the task and it takes O(n) time.
Auxiliary Space: O(1) constant additional space is required.

  Method #2 : Using statistics.harmonic_mean() This task can also be performed using inbuilt function of harmonic_mean(). This is new in Python versions >= 3.8. 

Python3




# Python3 code to demonstrate working of
# Harmonic Mean of List
# using statistics.harmonic_mean()
import statistics
 
# initialize list
test_list = [6, 7, 3, 9, 10, 15]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Harmonic Mean of List
# using statistics.harmonic_mean()
res = statistics.harmonic_mean(test_list)
 
# printing result
print("The harmomin mean of list is : " + str(res))


Output : 

The original list is : [6, 7, 3, 9, 10, 15]
The harmonic mean of list is : 6.517241379310345

Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

 Method #3 : Using reduce+lambda

Approach

the reduce() function from the functools module along with a lambda function that performs the reciprocal sum of the list elements.

Algorithm

1. Start with an initial value of 0 for the reduction
2. For each number in the list, add 1 divided by the number to the reduction
3. Divide the length of the list by the result of the reduction to get the harmonic mean

Python3




# Import the reduce function from the functools module
from functools import reduce
 
# Define a list of numbers
numbers = [1, 2, 3, 4, 5]
 
# Calculate the harmonic mean of the numbers using the reduce function and lambda function
# The reduce function takes a function and applies it cumulatively to the elements of a list
# In this case, the lambda function takes two arguments (x and y) and returns the sum of x and 1/y
# The reduce function starts with an initial value of 0 and applies the lambda function to each element of the numbers list
# Finally, the harmonic mean is calculated by dividing the length of the numbers list by the result of the reduce function
hm = len(numbers) / reduce(lambda x, y: x + (1/y), numbers, 0)
 
# Print the harmonic mean
print(hm)


Output

2.18978102189781

Time complexity: O(n), where n is the length of the numbers list. The lambda function has a constant time complexity of O(1), as it performs a simple arithmetic operation.

Auxiliary Space: O(n),where n is the length of the numbers list. This is because the reduce() function creates an intermediate result for each element of the numbers list, which requires additional memory.

 Method #4: Using list comprehension:

Algorithm:

1.Initialize the list.
2.Calculate the reciprocal of each element in the list.
3.Sum all the reciprocals.
4.Divide the length of the list by the sum obtained in step 3.
5.The result obtained in step 4 is the harmonic mean of the list.

Python3




# initialize list
test_list = [6, 7, 3, 9, 10, 15]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Harmonic Mean of List
# using list comprehension + formula
res = len(test_list) / sum([1 / ele for ele in test_list])
 
# printing result
print("The harmonic mean of list is: " + str(res))
#This code is contributed by Jyothi pinjala


Output

The original list is : [6, 7, 3, 9, 10, 15]
The harmonic mean of list is: 6.517241379310345

Time Complexity:  O(n), where n is the length of the list. This is because the algorithm iterates through the list once to calculate the reciprocal of each element and then sums the reciprocals.

Auxiliary Space:  O(1), because the algorithm only uses a constant amount of extra memory to store the sum and the length of the list.



Last Updated : 17 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads