Skip to content

Sort a dictionary by key in Python


dictionary = {'one': 1, 'three': 3, 'five': 5, 'two': 2, 'four': 4}
print(dict(sorted(dictionary.items(), reverse = False)))

#{'four': 4, 'five': 5, 'three': 3, 'two': 2, 'one': 1}

See also  How to clear the screen in Python turtle?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.