Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do you sort a dictionary by key from highest to lowest

Hi I have a dictionary that is currently the one below:

dictionary={1:'a',2:'b',3:'c'}

my desired output is to have a dictionary with the highest keys to the left and the lowest keys to the right like below:

dictionary={3:'c',2:'b',1:'a'}

how do you do that in python 3.7?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

dict(sorted(dictionary.items(), reverse=True))

This is duplicate of How do I sort a dictionary by key?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading