Dark Mode On/Off

# Python program to Sort Dictionary key and values List

In this tutorial, we will learn to write a program in Python to sort a dictionary using the key while also sorting the values in the list of each key. For a given dictionary with keys and values which are given in a list, we will be displaying output as follows.

Input: {'z': [12, 3], 'x': [4, 9, 1], 'y': [11, 4]}

Output: {'x': [1, 4, 9], 'y': [4, 11], 'z': [3, 12]}

Input: {'b': [2, 7, 5, 1, 4], 'a': [20, 16, 2, 4]}

Output: {'a': [2, 4, 16, 20], 'b': [1, 2, 4, 5, 7]}

To solve this problem, we can follow these approaches-

1. using sorted() method and loop
2. using sorted() and dictionary comprehension

## Approach 1: sorted() method and loop

In this approach first, we will sort all the keys in the dictionary, and then we will perform sorting on the list values using a loop. To sort keys we will use the `sorted()` method that will return all keys after sorting them alphabetically or in ascending order.

### Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a dictionary with values

Step 2- Print the original dictionary

Step 3- Declare an empty dictionary that will store the sorted dictionary

Step 4- Sort keys using` sorted() `

Step 5- Run a loop to access keys in the sorted order

Step 6- In the loop sort the value list of each key and store it in the new dictionary

Step 7- Print the new dictionary as the result

### Python Program 1

Look at the program to understand the implementation of the above-mentioned approach.

``````dic = {'study': [5, 3, 7],
'to': [11, 4, 2, 6],
'night': [10, 9]}

# print original dictionary
print("The original dictionary is : " ,dic)

# Sort Dictionary key and value List
# Using sorted() and loop
sort_dict = {}
for key in sorted(dic):
sort_dict[key] = sorted(dic[key])

# printing result
print("The sorted dictionary : " , sort_dict) ``````

The original dictionary is : {'study': [5, 3, 7], 'to': [11, 4, 2, 6], 'night': [10, 9]}
The sorted dictionary : {'night': [9, 10], 'study': [3, 5, 7], 'to': [2, 4, 6, 11]}

## Approach 2: sorted() method and dictionary comprehension

We will follow a similar approach as mentioned before of sorting the keys first and then sorting the value list. We will be using dictionary comprehension to store sorted values in a new dictionary. The dictionary comprehension is a shorter syntax for creating a new dictionary based on the values of an existing dictionary.

### Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a dictionary with values

Step 2- Print the original dicitonary

Step 3- Use dictionary comprehension to store sorted values in the dictionary

Step 4- Sort keys and values using the `sorted() `method

Step 5- Print the new dictionary as the result

### Python Program 2

Look at the program to understand the implementation of the above-mentioned approach.

``````dic = {'p': [11, 2],
'r': [3, 2, 1],
'q': [7, 4]}

# print original dictionary
print("The original dictionary is : " ,dic)

# Sort Dictionary key and value List
# dictionary comprehension
sort_dict = {key: sorted(dic[key]) for key in sorted(dic)}

# printing result
print("The sorted dictionary : " , sort_dict) ``````

The original dictionary is : {'p': [11, 2], 'r': [3, 2, 1], 'q': [7, 4]}
The sorted dictionary : {'p': [2, 11], 'q': [4, 7], 'r': [1, 2, 3]}

## Conclusion

In this tutorial, we have learned how to sort keys and the value list of a dictionary using methods in Python. We have used the `sorted() `method to sort keys and values of the given dictionary.

Want to learn coding?
Try our new interactive courses.
Over 20,000+ students enrolled.