Reputation: 29
Please, I am told to find the maximum value of the list of keys in python but before that I was told to "create and sort a list of the dictionary's keys" and I did it with this code
sorted_keys = sorted(verse_dict.items(), key = lambda t: t[0])
But now, I am told to find the element with the highest value in the list of keys that I created.
Upvotes: 1
Views: 1413
Reputation: 1
Simply, get the last value of the sorted keys:
sorted_keys = sorted(verse_dict.keys())
sorted_keys[-1]
Upvotes: -2
Reputation: 463
In your question, you have sorted your dictionary according to the key
, now if you want to sort it by value
, you can sort it like this
sorted(verse_dict.items(), key = lambda t: t[1])
Just like how Paul Lo mentioned it.
But now, since you are supposed to find the key having highest value, what you need to do is access the last element using indexing, you can update the above code like this:
sorted(verse_dict.items(), key = lambda t: t[1])[-1][0]
And, you'll get the highest value containing key in your dictionary.
Hope it helps, thanks.
Upvotes: 1
Reputation: 6148
Changing the indexing to t[1]
(value) from t[0]
(key) should work for your case:
sorted(verse_dict.items(), key = lambda t: t[1])
Upvotes: 2