How to Sort Dictionary in Python by Value
A dictionary in Python is a collection of items that stores data as key-value pairs.
We have used the sorted()
function and lambda
for sorting the dictionary by values
d = {1: 10, 2: 9, 3: 8, 4: 7} print({k: v for k, v in sorted(d.items(), key = lambda item : item[1])})
Output:
{4: 7, 3: 8, 2: 9, 1: 10}
Subscribe
Login
Please login to comment
0 Discussion