Python Forum
Finding average in dictionary using for loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding average in dictionary using for loop
#5
You can use the statistics module.
A cool feature is, that you can use also Fraction. Math teacher will love this.

import statistics


data = {
    'Jeremy':73284, 
    'Hansel':8784.3, 
    'Uee':9480938.2, 
    'Seolhyun':984958.3, 
    'Ketsuno Ana':24131, 
    'Trump':45789
}


mean = statistics.mean(data.values()) 
median = statistics.median(data.values())
h_mean = statistics.harmonic_mean(data.values())

print("The mean networth is:", mean)
print("The median networth is:", median)
print("The h_mean networth is:", h_mean)
You can use formatting (>= Python 2.7):

print("The mean networth is: {:.2f}".format(mean))
print("The median networth is: {:.2f}".format(median))
print("The h_mean networth is: {:.2f}".format(h_mean))
Output:
The mean networth is: 1769647.47 The median networth is: 59536.50 The h_mean networth is: 31268.64
And since Python 3.6 you could use Literal String Interpolation, better known as format strings. The syntax is equal, the only difference is, that the interpolation is accessing local variables.

print(f"The mean networth is: {mean:.2f}")
print(f"The median networth is: {median:.2f}")
print(f"The h_mean networth is: {h_mean:.2f}")
Output:
The mean networth is: 1769647.47 The median networth is: 59536.50 The h_mean networth is: 31268.64
By the way, other functions you can use on the ValuesView, which is an Iterable.
data = {
    'Jeremy':73284, 
    'Hansel':8784.3, 
    'Uee':9480938.2, 
    'Seolhyun':984958.3, 
    'Ketsuno Ana':24131, 
    'Trump':45789
}


dict_values = data.values()
# the dict_values is the view to the values of the dictionary
# it's accessing the original dict
# if you update a value or add a key with a value
# the view is updated

print("Before Update")
min_val, max_val = min(dict_values), max(dict_values)
print(f"networth min: {min_val:.2f}\nnetworth max: {max_val:.2f}")

# calculate and print values, before updating
mean = statistics.mean(dict_values) 
median = statistics.median(dict_values)
h_mean = statistics.harmonic_mean(dict_values)
print(f"The mean networth is: {mean:.2f}")
print(f"The median networth is: {median:.2f}")
print(f"The h_mean networth is: {h_mean:.2f}")

# updating the dict
data["Angela Merkel"] = -666

print("After Update")
min_val, max_val = min(dict_values), max(dict_values)
print(f"networth min: {min_val:.2f}\nnetworth max: {max_val:.2f}")
mean = statistics.mean(dict_values) 
median = statistics.median(dict_values)
# h_mean = statistics.harmonic_mean(dict_values)
# StatisticsError: harmonic mean does not support negative values
print(f"The mean networth is: {mean:.2f}")
print(f"The median networth is: {median:.2f}")
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Finding average in dictionary using for loop - by DeaD_EyE - May-26-2019, 12:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop through elements of list and include as value in the dictionary Rupini 3 2,662 Jun-13-2020, 05:43 AM
Last Post: buran
  Finding the average of numbers in a txt file piday 1 19,222 Feb-27-2018, 04:00 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020