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
#1
So I'm a super newbie in python, and I'm taking a course right now and this is my homework. I'm supposed to get the average using a for loop, and I know I'm wrong but I don't know how to do it? :( Below is my code as of now!

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

total_networth = 0
total_number_of_people = 0

for row in data.items():
    total_networth += networth
    total_number_of_people += 1
    
average = total_networth/total_number_of_people

print("The average networth is $" + str(round(average,2)))
Reply
#2
You could check this out: https://www.w3schools.com/python/python_...naries.asp

for value  in thisdict.values():
  print(value) 

for key, value in thisdict.items():
  print(key, value) 
Reply
#3
items gives you the (key, value) pairs from the dictionary. You want the values method, which in your case would just give you the number values that you are trying to average.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
Thank you!
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop through elements of list and include as value in the dictionary Rupini 3 2,631 Jun-13-2020, 05:43 AM
Last Post: buran
  Finding the average of numbers in a txt file piday 1 19,191 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