 # Solution Sharing

Alternatively you could use the following:

10 Likes

Sorta took a long-handed approach lol. Couldn’t work out how to get a key from a value but got it in the end by using a for loop.

``````def max_key(my_dictionary):
lst1=[]
for keys in my_dictionary.keys():
lst1.append(my_dictionary[keys])
newlst=sorted(lst1)
print(newlst)
maxkey=newlst[-1]
for keys,values in my_dictionary.items():
if my_dictionary[keys]==maxkey:
return keys
``````
1 Like
``````def max_key(my_dictionary):
largest_key = "" # initialize a blank string
largest_value = -100000 # initialize a very low valued number
for i in my_dictionary: # loop through the dictionary
if my_dictionary[i] > largest_value: # using an if-statement to determine whats the max value
largest_value = my_dictionary[i] # assinging largest_value variable to be our largest number "value"
largest_key = i # assigning largest_key to be the result of whatever key had the largest value
return largest_key

print(max_key({1:100, 2:1, 3:4, 4:10}))
# prints 1
print(max_key({"a":100, "b":10, "c":1000}))
# prints "c"
``````
2 Likes

I like you solution better than mine, which was:

``````def max_key(d):
max_value = max(d.values())
for key, value in d.items():
if value == max_value:
return key
``````
1 Like

`max` has a parameter for doing something with each value before comparing

``````def max_key(d):
return max(d, key=d.get)
``````

You can do the same thing with sort. (and min, obviously)

``````def longest_word(words):
return max(words, key=len)
``````
4 Likes
``````def max_key(my_dictionary):
x = list(my_dictionary.values())
while len(x)>1:
if x >= x:
x.pop(1)
elif x < x:
x.pop(0)
v = x
for keys, values in my_dictionary.items():
if v == values:
return keys
``````

I like how everyone had their own way of doing things and all of them were less winded than mine, lol.

1 Like

Wow - seems like everyone came up with something different. Here’s what I got:

``````def max_key(my_dictionary):
largest_key = ""
largest_value = float("-inf")`Preformatted text`
for key, value in my_dictionary.items():
if value > largest_value:
largest_value = value
largest_key = key
return largest_key
``````

def max_key (my_dictionary):
largest_key = “”
largest_value = float("-inf")
for i in my_dictionary:
if my_dictionary[i] > largest_value: ## Doesn’t this iterate through the keys, how is it seeing the value?

Sticking only to what’s been covered in this course (i.e. not using max()):

``````def max_key(my_dictionary):
for i in my_dictionary:
if my_dictionary[i] == sorted(list(my_dictionary.values()))[-1]:
return i
``````
5 Likes

My very messy and long winded approach!:

``````def max_key(my_dictionary):
max_value = 0
max_key = None
for key, value in my_dictionary.items():
if value > max_value:
max_value = value
for key, value in my_dictionary.items():
if value == max_value:
print(key)
max_key = key
return max_key
``````

I can’t remember if we’ve been shown the max() function yet but I like your solution a lot! Very simple, elegant and easy to follow.

Hi! I was confused about that one, too. I think that’s because I mix it up with index and lists. Here, we’re working with dictionaries, so when we call on the key, it gives us the value of that key in the dictionary. Below is an example:

d1 = {1:100, 2:1, 3:4, 4:10}

for i in d1:

`````` print(d1[i])
``````

Output: 100
1
4
10

I decided not to use max() and I came up with:

``````def max_key(my_dictionary):
maxx = list(my_dictionary.values())
maxxind = 0
for i in range(len(list(my_dictionary.values()))):
if list(my_dictionary.values())[i] > maxx:
maxx = list(my_dictionary.values())[i]
maxxind = i
return list(my_dictionary.keys())[maxxind]
``````

each time you create a list of the values you’re making a copy of the whole structure, you might want to avoid that

the values of a dict is iterable, so you could iterate through them instead, or convert to list once and use that list, but there’s no reason to since you can iterate through the values, which is why you’d want a list

I had used it as I wanted to use indices and using it with the dict_values brought up an error. So I thought using a list would be easier as I’m more familiar with those. But, yes I see your point. Thanks!

Does anybody else feel suddenly and massively out of their depth? I’ve absorbed and understood everything in the course so far, but it seems everything I try to do with dictionaries just doesn’t work. Every solution I see seems utterly counter-intuitive and any progress I’ve made on this module has been through guesswork and trial-and-error.

Has it been poorly presented, or it is just me?

3 Likes
``````def max_key(my_dictionary):

m_key = 0

for key,value in my_dictionary.items():

if value > my_dictionary.get(m_key,0):

m_key = key

return m_key
``````

What if there are no values greater than or equal to zero? In this instance we should not have a default value. Likewise, `m_key` should be a value from the dictionary, not `0`.

1 Like

Thank you! I will review my answer and re-post.

mine was
i had to look the hint cuz i was stuck at “how do i do the lowest value possible for my variable?”

``````def max_key(my_dictionary = {}):
largest_value = float("-inf")
for key, value in my_dictionary.items():
if value > largest_value:
largest_value = value
highest_key = key
return highest_key
``````

was that max() function presented before? i like it ;D