Python | Sort the items alphabetically from given dictionary

Given a dictionary, write a Python program to get the alphabetically sorted items from given dictionary and print it. Let’s see some ways we can do this task.

Code #1: Using dict.items()

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to sort the items alphabetically from given dictionary
  
# initialising _dictionary 
dict = {'key1' : 'AGeek', 'key2' : 'For', 'key3': 'IsGeeks''key4': 'ZGeeks'
  
# printing iniial_dictionary 
print ("Original dictionary", str(dict)) 
  
# getting items in sorted order 
print ("\nItems in sorted order"
for key, value in sorted(dict.items()): 
    print(value) 

chevron_right


Output:



Original dictionary {‘key4’: ‘ZGeeks’, ‘key2’: ‘For’, ‘key1’: ‘AGeek’, ‘key3’: ‘IsGeeks’}

Items in sorted order
AGeek
For
IsGeeks
ZGeeks

 

Code #2: Using sorted()

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to sort the items alphabetically from given dictionary
  
# initialising _dictionary 
dict = {'key1' : 'AGeek', 'key2' : 'For', 'key3': 'IsGeeks''key4': 'ZGeeks'
  
# printing iniial_dictionary 
print ("Original dictionary", str(dict)) 
  
# getting items in sorted order 
print ("\nItems in sorted order"
for key in sorted(dict): 
    print (dict[key]) 

chevron_right


Output:

Original dictionary {‘key4’: ‘ZGeeks’, ‘key2’: ‘For’, ‘key1’: ‘AGeek’, ‘key3’: ‘IsGeeks’}

Items in sorted order
AGeek
For
IsGeeks
ZGeeks



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.