Skip to content

Sorting a dictionary by key in Python

scores = {'Naveen' : 5,
          'Arthi' : 2,
          'Subash' : 4}
for key in sorted(scores):
    print("%s: %s" % (key, scores[key]))
dict = {2: 3, 1: 89, 4: 5, 3: 0}
od = sorted(dict.items())
print(od)
See also  Unique Email Addresses - Leetcode Challenge - Python Solution

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.