orders = {
'apple: 54,
'banana': 56,
'orange': 72,
'peach': 48,
'grape': 41
}
Given a dictionary in this format, how can I sort the dictionary in descending order by values?
orders = {
'apple: 54,
'banana': 56,
'orange': 72,
'peach': 48,
'grape': 41
}
Given a dictionary in this format, how can I sort the dictionary in descending order by values?
You can use the sorted function to do it.
orders = {
'apple': 54,
'banana': 56,
'orange': 72,
'peach': 48,
'grape': 41
}
d = sorted(orders.items(), key=lambda x:x[1], reverse=True)
print(dict(d))
output:
{'orange': 72, 'banana': 56, 'apple': 54, 'peach': 48, 'grape': 41}
sort_orders = sorted(orders.items(), key=lambda x: x[1], reverse=True)