There is one package known as the operator, from that, we get one function known as itemgetter() function.
This function will help us to get the object that will return a single element from an object.
For example:
import operator
dict1 = {1: 3, 2: 6}
get_item_with_key_2 = operator.itemgetter(2)
print(get_item_with_key_2(dict1))
From the above code, we will get the main element from the '2' key value.
Now we will use the items() function returns the key-value pairs of a dictionary in the form of a list of tuples values.
After that, we can arrange the list of tuples values by executing the itemgetter() function so that it can extract the second value of the tuple, that is all the values from the keys section from the dictionary.
Once all the values are in an arranged manner, we can simply create a dictionary based on those values. Refer to the below code:
import operator
d1 = {1: 1, 2: 9, 3: 4}
sort = sorted(d1.items(), key=operator.itemgetter(1))
print(sort) # [(1, 1), (3, 4), (2, 9)]
sorted_dict = {k: v for k, v in sort}
print(sorted_dict) # {1: 1, 3: 4, 2: 9}
If you want to know more about Python programming, then refer to the course that will help you out in a better way.