FAQ: Code Challenge: Dictionaries - Largest Value

The reasonable way to express maximum value of a dict is:


which raises questions about what’s okay to use, so, maybe:

maximum = next(iter(d.values())
for v in d.values():
    if v > maximum:
        maximum = v
return maximum
1 Like

Not how the hint described the outcome but it seems I got the correct answer with this code. The green check mark showed up so I think this also works, but I could be missing something. I found this way because I didn’t want to look at the hint and did know about the float("-inf") method.

def max_key(my_dictionary):
  maximum = list(my_dictionary.values())
  for key in my_dictionary:
    if my_dictionary[key] == max(maximum):
      return key

That’s actually working:

def max_key(my_dictionary):

  return max(my_dictionary, key=my_dictionary.get)

It’s so helpful how the hint tells you to use an empty string for largest_key, yet the answer says differently. Did everything right except that part. Thanks a bunch…

def max_key(my_dictionary):
  top_value = max_num(my_dictionary.values())
  for (key, value) in my_dictionary.items():
    if value in top_value:
      return key

def max_num(x):
  list = [-1000]
  for item in x:
    if item >= list[0]:
      list[0] = item
  return list