0

I have the dictionary as shown below and I want to pick the key whose value is the maximum in dictionary

dictionary = {'george': 16, 'amber': 19 ,'leper':23}

Expected_Output: leper
Hamza
  • 530
  • 5
  • 27
  • 2
    Does this answer your question? [Getting key with maximum value in dictionary?](https://stackoverflow.com/questions/268272/getting-key-with-maximum-value-in-dictionary) – ViaTech Jun 19 '21 at 11:42

2 Answers2

1

Try this:

max_key = max(dictionary, key=dictionary.get)
print(max_key)
HARSH MITTAL
  • 750
  • 4
  • 17
0

This may not be the best method to get the answer when it comes to time complexity

dictionary = {'george': 16, 'amber': 19, 'leper': 23}

# Creating the list of values
values = [i for i in dictionary.values()]

# Creating the list of keys

keys = [i for i in dictionary.keys()]

# Declaring the g [greater value]

g = int

# iterating through the list to find the maximum value

for i in range(len(values)):
    try:
        temp_i = values[i+1]
    except IndexError:
        temp_i = values[i]
    for i in values:
        if temp_i > i:
            g = temp_i
        else:
            g = i

pos = values.index(g)
print(keys[pos])
 
XBOT_ADMIN
  • 61
  • 3