 # Solution Sharing

The thing to bear in mind is the maximum number iterations necessary to complete this task. It is not necessary to do any sorting. That just adds to the complexity.

Spoiler
``````>>> def max_key(d):
key = [*d.keys()]
val = d.get(key)
for k, v in d.items():
if v > val:
key, val = k, v
return key, val

>>> a = {'one': 1, 'two': 2, 'three': 3}
>>> max_key(a)
('three', 3)
>>>
``````

Rabbit Hole
``````>>> def max_key(d):
try:
key = [*d.keys()]
val = d.get(key)
for k, v in d.items():
if v > val:
key, val = k, v
return key, val
except IndexError:
return "Empty dictionary, perhaps?"
except:
return "Fatal Error!"

>>> max_key({})
'Empty dictionary, perhaps?'
>>> max_key(n)
'Fatal Error!'
>>> max_key(a)
('three', 3)
>>>
``````

So many ways.

``````def max_key(my_dictionary):
def second(elem):
return elem
lst = sorted(list(my_dictionary.items()), key=second)
return lst[-1]
``````

Very elegant solution!

Unless I’m mistaken, your solution is actually more efficient that the one by prosowski because it only calls `.values()` once, instead of calling it once for each item in `my_dictionary`. Granted, I don’t think `.values()` is very “expensive”.

I saw many different ways of doing this task, this is mine:

``````def max_key(my_dictionary):

max_value = 0

for i, j in my_dictionary.items():

if j > max_value:

max_value = j
key_max_value = i

else:
continue

return key_max_value
``````

What if all the values are negative?