In this python dictionary program, we will sort a dictionary using keys.
Steps to solve the program
- Take a dictionary as input.
- Sort the given dictionary by keys using sorted() method.
- Print the output.
dict1 = {'d':21,'b':53,'a':13,'c':41}
for key in sorted(dict1):
print("%s: %s" % (key,dict1[key]))
Output :
a: 13
b: 53
c: 41
d: 21