How to sort multi-level dictionaries by it's value?

How to sort multi-level dictionaries by it's value?



I just wanted to sort this dict by value of runs scored by the player, but I'm not getting it how to do this.


orangecap('test1':'Ashwin':84, 'Kohli':120, 'test2':'ashwin':59, 'Pujara':42)



here "orangecap" is the def for the function to be called, and I want to return the name of highest scorer's name (in this case: Kohli)..



Can this be done without using external libraries?





Those are dicts, not lists.
– Aran-Fey
Sep 5 '18 at 6:11





Are you trying to sort or find the name with the max value? your title doesn't match your description. You should show what you've tried.
– Sayse
Sep 5 '18 at 6:15




2 Answers
2



Setup


dict_of_dicts =
'test1': 'Ashwin' :84, 'Kohli': 120,
'test2': 'ashwin': 59, 'Pujara': 42,
'test3': 'Pandas': 120, 'R': 119



Solution



It appears that you have a dict of dicts (not a list). You need to iterate through each value in the list and see if it is higher that the max value (initially set to None, along with the name of the player and the first key, e.g. test1, test2, etc.). If it is, reset their values to the new max value and append the player name and key to the list holding those who obtained the max value (to account for ties, e.g. Kohli & Pandas).


test1


test2


def orangecap(dict_of_dicts):
max_key_name =
max_score = None
for key in dict_of_dicts:
for name, score in dict_of_dicts[key].iteritems(): # .items() in Python 3.
if score > max_score:
max_key_name = [(key, name)]
max_score = score
elif score == max_score:
max_key_name.append((key, name))
return max_key_name, max_score

>>> orangecap(dict_of_dicts=dict_of_dicts)
([('test1', 'Kohli'), ('test3', 'Pandas')], 120)





what is "iteritems", I didn't get that.
– Hououin Kyouma
Sep 5 '18 at 6:45






Your question was tagged both Python 2.7 and Python 3.x, so I wasn't sure what version you are using. The behavior is the same, but the function name changed from iteritems in Python 2 to items in Python 3. This function returns an iterator of tuple pairs of keys and values in your dictionary, e.g. ('Adwin', 84), ('Kohli', 120), etc. stackoverflow.com/questions/10458437/…
– Alexander
Sep 5 '18 at 6:50



iteritems


items



A very simple solution for this would be to iterate over the dict of dict, use two variables to store runs and player, like this -


def orangecap(your_dict):
max_runs = 0
player_scored = ''

for match, stats in your_dict.items():
for player, runs in stats.items():
if runs > max_runs:
max_runs = runs
player_scored = player
return player_scored





"This answer is not useful" downvoter, please explain how?
– ThatBird
Sep 5 '18 at 6:21






Your answer doesn't sort the dictionary
– Sayse
Sep 5 '18 at 6:27





can you please explain this, I'm new to python and I didn't get it how it is working correctly.. and thanks for the answer. @ThatBird
– Hououin Kyouma
Sep 5 '18 at 6:30






@Sayse Sorting the dictionary is not a requirement but only a means to an end, which is to extract the max value and corresponding name.
– Alexander
Sep 5 '18 at 6:42





@Alexander - Without knowing the OP's full requirements, that is impossible to say for certain.
– Sayse
Sep 5 '18 at 6:44



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)