A dictionary in Python is an unsorted key-value pair data structure, where key-value pairs are comma-separated. It is useful for faster lookups.
This dictionary can be sorted based on the values using the ValueSortedDict method provided by the sortedcollections module.
ValueSortedDict(key value pairs)
This method takes a dictionary as a parameter.
This method returns a sorted dictionary.
In the following example, we will try to sort the students dictionary according to the students' ages.
from sortedcollections import ValueSortedDict#unsorted dictstudents = {"John":11, "Vicky": 10, "Riya":13, "Elina":9}#sort dict based on valuesprint(ValueSortedDict(students))
ValueSortedDict method from the sortedcollections module.students, which contains the student's name and their age as key-value pairs.students dictionary with the ValueSortedDict method. Next, we print the returned sorted dictionary.