- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sort Dictionary key and values List in Python
When it is required to sort the key and values in a dictionary, the ‘sorted’ method can be used.
Below is the demonstration of the same −
Example
my_dict = {'Hi': [1, 6, 3], 'there': [2, 9, 6], 'Mark': [16, 7]} print("The dictionary is : ") print(my_dict) my_result = dict() for key in sorted(my_dict): my_result[key] = sorted(my_dict[key]) print("The sorted dictionary is : " ) print(my_result)
Output
The dictionary is : {'Hi': [1, 6, 3], 'there': [2, 9, 6], 'Mark': [16, 7]} The sorted dictionary is : {'Hi': [1, 3, 6], 'Mark': [7, 16], 'there': [2, 6, 9]}
Explanation
A dictionary is defined, and is displayed on the console.
An empty dictionary is defined.
The dictionary is iterated over, before which it is sorted.
The key is again sorted and assigned to the empty dictionary.
The sorted dictionary is displayed on the console.
- Related Articles
- Convert key-values list to flat dictionary in Python
- Python - Filter dictionary key based on the values in selective list
- Python – Inverse Dictionary Values List
- Python – Extract Key’s Value, if Key Present in List and Dictionary
- How to sort a dictionary in Python by values?
- How do I sort a list of dictionaries by values of the dictionary in Python?
- Combining values from dictionary of list in Python
- Python – Sort Dictionary List by Key’s ith Index value
- Python program to get maximum of each key Dictionary List
- How to create Python dictionary from list of keys and values?
- Python – Limit the values to keys in a Dictionary List
- Python - Create a dictionary using list with none values
- Python - Filter dictionaries by values in Kth Key in list
- Python | Sort the values of first list using second list
- Append Dictionary Keys and Values (In order ) in dictionary using Python

Advertisements