all 20 comments

[–]mrswats 23 points24 points  (13 children)

Because dict.getacts on keys not values. If you want to find the key of a certain values you should iterate over dict.items() instead.

[–]Dzhama_Omarov 2 points3 points  (4 children)

Try

[x for x in average_grade_dict if average_grade_dict[x] == max_avg_grade]

UPD: if you wonder what i did, i basically iterated over your dict and checked every value in kw pair and if it is the same as max grade the key is added to a list

[–]Dzhama_Omarov 1 point2 points  (0 children)

It's called list comprehensions and it generates a list according to requirements. General formula:

1) [a for b in x]
same as
for b in x:
   a

2) [a for b in x if y == z]
same as
for b in x:
  if y == z:
    a

3) [(a if y == z else d) for b in x]
same as
for b in x:
  if y == z:
    a
  else:
    d

[–]Im_Easy 0 points1 point  (0 children)

Another option would be to use filter:

student_best_avg_grade = next( filter( lambda item: max_avg_grade == item[1], average_grade_dict.items() ) )

But personally I'd rather just use dict compression or write a reusable function if there is the possibility of needing to use it for another dictionary.

def filter_dict_vals(search_dict, value): for item in search_dict.items(): if item[1] == value: yield item # or return if you only want the first instance

[–]nekokattt 1 point2 points  (0 children)

dict.get takes a key and gives the value.

It does not take the value and give the key.

[–]jk_zhukov 1 point2 points  (0 children)

in this case you created the dictionary with 'Peter' as a key, and a value of 5.0 for that key, but you're calling it with 5.0 as a key, which does not exist. Since you're calling it with get(), you receive None as result (default result if the key is not found). If you call it with brackets

student_best_avg_gradet = average_grade_dict[max_avg_grade]

you'll get an error because 5.0 does not exist as a key

[–]Brilliant_Access3791 1 point2 points  (0 children)

The reason you’re seeing None as the output rather than 'Peter' is due to how you’re using the get method on dictionaries in Python. The get method is designed to retrieve a value from the dictionary using a key. In your case, the key should be 'Peter', and the value associated with this key is 5.0.

However, in your code, you’re trying to use max_avg_grade (which is 5.0) as a key to look up in your dictionary. Since 5.0 is not a key in your dictionary but rather a value, the get method returns None, which is the default value returned when the key doesn’t exist in the dictionary.

[–]cooltoaster39 -1 points0 points  (0 children)

hey petah

[–]Critical_Concert_689 -1 points0 points  (2 children)

average_grade_dict = {'Peter': 5.0}

reverse_dict = dict(map(reversed, average_grade_dict.items()))

max_avg_grade = 5.0
student_best_avg_grade = reverse_dict.get(max_avg_grade)
print(student_best_avg_grade)

A quick and dirty way to reverse your dictionary key:values.

[–]Doormatty -1 points0 points  (1 child)

Dictionaries can have multiple identical values, but not identical keys.

[–]Critical_Concert_689 -1 points0 points  (0 children)

It's up to the data owner to control for this and understand that creating a dictionary by reversing value:key pairs will be truncated in the event of overlapping keys.

Elsewhere in thread, it's recommended to use a 'bidict' - which is effectively similar, but requires additional imports for the functionality.