-1

I have a dictionary contains keys and values:

dic1 = {'first': 13, 'second': 7, 'third': 5}

I want to compare the values and select key with the largest number: The output should be:

'first'

here is my code:

import operator
dic1 = {'first': 13, 'second': 7, 'third': 5}
total = [k:max(dic1.values()) for k,v in dic1.items()]

but I got SyntaxError.. any help?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Yousra Gad
  • 363
  • 3
  • 15
  • Hint: What is that `]` doing there when you're defining `dic1`? Also, for your `total =` line, it looks like you're trying to do a dictionary comprehension using list comprehension syntax – sacuL Oct 21 '18 at 18:02

1 Answers1

0

You can do:

max(dic1.items(), key=lambda key_value_pair: key_value_pair[1])[0]

The call to max returns a tuple (key, value), and then you get the first entry in this tuple with max(...)[0].

ForceBru
  • 43,482
  • 10
  • 63
  • 98