Python program to sort a dictionary using keys.

In this python dictionary program, we will sort a dictionary using keys.

Steps to solve the program
  1. Take a dictionary as input.
  2. Sort the given dictionary by keys using sorted() method.
  3. 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
				
			

create a dictionary from the string.

sort a dictionary in python using values.

Leave a Comment