Open In App

Python | Sort the list alphabetically in a dictionary

Last Updated : 12 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Python Dictionary is quite a useful data structure, which is usually used to hash a particular key with value, so that they can be retrieved efficiently. Let’s see how to sort the list alphabetically in a dictionary.

Sort a List Alphabetically in Python

In Python, Sorting a List Alphabetically is a typical activity that takes on added interest when working with a list contained within a dictionary. This article will examine various methods for sorting a list within a Python dictionary alphabetically. List comprehension, lambda functions, and techniques utilizing the sorted() function will all be covered.

  • Using Sorted() Function
  • Using List Comprehension
  • Using Lambda Functions

Sort using Sorted() Function

The built-in sorted() function of Python enables us to Sort a List Alphabetically. This function and lambda functions can be used to order the list inside a dictionary.

Python3




my_dict = {
    "L1": ["Red", "Green", "Yellow", "Blue"],
    "L2": ["Violet", "Dark green", "Pink", "White"],
    "L3": ["Black", "Aqua", "Beige", "Aqua"],
    "L4": ["Alice Blue", "Cyan", "Gold", "Silver"]
}
print("Before Sorting:")
for key, value in my_dict.items():
    print(key + ": " + str(value))
 
sorted_dict = {key: sorted(value) for key, value in my_dict.items()}
print("\nAfter Sorting:")
for key, value in sorted_dict.items():
    print(key + ": " + str(value))


Output

Before Sorting:
L1: ['Red', 'Green', 'Yellow', 'Blue']
L2: ['Violet', 'Dark green', 'Pink', 'White']
L3: ['Black', 'Aqua', 'Beige', 'Aqua']
L4: ['Alice Blue', 'Cyan', 'Gold', 'Silver']
After Sorting:
L1: ['Blue', 'Green', 'Red', 'Yellow']
L2: ['Dark green', 'Pink', 'Violet', 'White']
L3: ['Aqua', 'Aqua', 'Beige', 'Black']
L4: ['Alice Blue', 'Cyan', 'Gold', 'Silver']

Time complexity: O(n*n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), to store the keys and values in the dictionary.

Sort using List Comprehension

A quick approach to going through the dictionary, Sorting a List Alphabetically, and making a new dictionary is by LIst comprehension.

Python3




my_dict = {
    "L1": ["Red", "Green", "Yellow", "Blue"],
    "L2": ["Violet", "Dark green", "Pink", "White"],
    "L3": ["Black", "Aqua", "Beige", "Aqua"],
    "L4": ["Alice Blue", "Cyan", "Gold", "Silver"]
}
 
# Print the original dictionary
print("Before Sorting:")
for key, value in my_dict.items():
    print(key + ": " + str(value))
 
# Sort the lists alphabetically within the dictionary using list comprehensions
sorted_dict = {key: sorted(value) for key, value in my_dict.items()}
 
# Print the sorted dictionary
print("\nAfter Sorting:")
for key, value in sorted_dict.items():
    print(key + ": " + str(value))


Output

Before Sorting:
L1: ['Red', 'Green', 'Yellow', 'Blue']
L2: ['Violet', 'Dark green', 'Pink', 'White']
L3: ['Black', 'Aqua', 'Beige', 'Aqua']
L4: ['Alice Blue', 'Cyan', 'Gold', 'Silver']
After Sorting:
L1: ['Blue', 'Green', 'Red', 'Yellow']
L2: ['Dark green', 'Pink', 'Violet', 'White']
L3: ['Aqua', 'Aqua', 'Beige', 'Black']
L4: ['Alice Blue', 'Cyan', 'Gold', 'Silver']

Time Complexity: O(n*m log m), where ‘m’ is the length of the longest list of values in the dictionary.
Space Complexity: O(n), where ‘n’ is the number of key-value pairs in the dictionary.

Sort using Lambda Function

To Sort a List Alphabetically, Within the sorted() function, lambda functions provide an alternate method for specifying sorting criteria.

Python3




my_dict = {
    "L1": ["Red", "Green", "Yellow", "Blue"],
    "L2": ["Violet", "Dark green", "Pink", "White"],
    "L3": ["Black", "Aqua", "Beige", "Aqua"],
    "L4": ["Alice Blue", "Cyan", "Gold", "Silver"]
}
 
print("Before Sorting:")
for key, value in my_dict.items():
    print(key + ": " + str(value))
 
sorted_dict = {key: sorted(value, key=lambda x: x.lower())
               for key, value in my_dict.items()}
 
print("\nAfter Sorting:")
for key, value in sorted_dict.items():
    print(key + ": " + str(value))


Output

Before Sorting:
L1: ['Red', 'Green', 'Yellow', 'Blue']
L2: ['Violet', 'Dark green', 'Pink', 'White']
L3: ['Black', 'Aqua', 'Beige', 'Aqua']
L4: ['Alice Blue', 'Cyan', 'Gold', 'Silver']
After Sorting:
L1: ['Blue', 'Green', 'Red', 'Yellow']
L2: ['Dark green', 'Pink', 'Violet', 'White']
L3: ['Aqua', 'Aqua', 'Beige', 'Black']
L4: ['Alice Blue', 'Cyan', 'Gold', 'Silver']

Time Complexity: O(n*m log m)
Space Complexity: O(n)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads