Alternatively you could use the following:

# Solution Sharing

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
```

```
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"
```

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
```

`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)
```

```
def max_key(my_dictionary):
x = list(my_dictionary.values())
while len(x)>1:
if x[0] >= x[1]:
x.pop(1)
elif x[0] < x[1]:
x.pop(0)
v = x[0]
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.

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
```

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