Answers for "desc sort dictionary by value python"

12

how to sort a dictionary by value in python

import operator
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=operator.itemgetter(1))


# Sort by key
import operator
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=operator.itemgetter(0))
Posted by: Guest on November-27-2019
1

sort dict by value

dict(sorted(x.items(), key=lambda item: item[1]))
Posted by: Guest on March-17-2021

Code answers related to "desc sort dictionary by value python"

Python Answers by Framework

Browse Popular Code Answers by Language