Example 1: Sort the dictionary based on values
Output
{6: 3, 5: 4, 1: 6}
- Here,
key=lambda item: item[1]returns the values of each key:value pair. - From each key:value pair of
dt.item(),sorted()sorts the items based on values.
Learn more about sorted() and its parameter key at Python sorted().
Example 2: Sort only the values
Output
[3, 4, 6]
In this example, sorted() is used for sorted values only. The values are fed into sorted() using dt.values().