0

The dictionary:

sales_year = {"1998": 3124, "1999": 234, "2001": 7214, "2002": 789}

How can I return the key-value pair of which the value is the highest? In this case it should return:

("2001", 7214)

Is there any function like "max()" which is as simple as possible?

cloudwave
  • 1
  • 1

1 Answers1

0

Buddy Bob's homemade approach. Find max value, iterate through dicts keys, locate certain key.

sales_year = {"1998": 3124, "1999": 234, "2001": 7214, "2002": 789}
for k in sales_year.keys():
    if sales_year[k] == max(sales_year.values()):
        print(k,max(sales_year.values()))
        break
Buddy Bob
  • 5,829
  • 1
  • 13
  • 44