1

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.

picture here

Sarques
  • 465
  • 7
  • 17
  • Does this answer your question? [Python 3 sort a dict by its values](https://stackoverflow.com/questions/20944483/python-3-sort-a-dict-by-its-values) – Boris Verkhovskiy Dec 11 '19 at 05:22

3 Answers3

2

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])
Paul Lo
  • 6,032
  • 6
  • 31
  • 36
1

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.

Sarques
  • 465
  • 7
  • 17
-2

Simply, get the last value of the sorted keys:

sorted_keys = sorted(verse_dict.keys())
sorted_keys[-1]
Tyler2P
  • 2,324
  • 26
  • 22
  • 31