Sunday, February 8, 2026
HomeLanguagesPython | Sort the items alphabetically from given dictionary

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() 

Python3




# Python program to sort the items alphabetically from given dictionary
 
# initialising _dictionary
dict = {'key2' : 'For', 'key3': 'IsGeeks''key1' : 'AGeek''key4': 'ZGeeks'}
 
# printing initial_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)


Output: 

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

Items in sorted order
AGeek
For
IsGeeks
ZGeeks

Time Complexity: O(n*logn)
Auxiliary Space: O(n)

Code #2: Using sorted() 

Python3




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


Output: 

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

Items in sorted order
AGeek
For
IsGeeks
ZGeeks

Time Complexity: O(n*logn)
Auxiliary Space: O(n)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32493 POSTS0 COMMENTS
Milvus
126 POSTS0 COMMENTS
Nango Kala
6864 POSTS0 COMMENTS
Nicole Veronica
11990 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12084 POSTS0 COMMENTS
Shaida Kate Naidoo
7000 POSTS0 COMMENTS
Ted Musemwa
7241 POSTS0 COMMENTS
Thapelo Manthata
6951 POSTS0 COMMENTS
Umr Jansen
6936 POSTS0 COMMENTS