# create a dictionary
my_dict = {'c': 3, 'd': 4, 'a': 1, 'b': 2, 'f': 6, 'e': 5}
# sort the dictionary by values
result = dict(sorted(my_dict.items(), key=lambda x: x[1]))
# print the result
print(result)
Output
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}
We use the sorted() function to sort a dictionary or list in Python. It takes the dictionary and key as a required parameter and returns the sorted dictionary. We are passing Dictionary.items() in the first argument of the sorted() function and in the key parameter we are passing a lambda function.
By default, it will sort the dictionary in ascending order. To sort the dictionary in descending order you will have to pass reverse=True in the sorted() function.
Code example: Sort a dictionary by value in ascending order
# create a dictionary
scores = {'math': 90, 'english': 80, 'physics': 85, 'chemistry': 75}
# sort the dictionary by values
result = dict(sorted(scores.items(), key=lambda x: x[1]))
# print the result
print(result)
Output
{'chemistry': 75, 'english': 80, 'physics': 85, 'math': 90}
In the above code example,
Code example: Sort a dictionary by value in descending order
# create a dictionary
scores = {'math': 90, 'english': 80, 'physics': 85, 'chemistry': 75}
# sort the dictionary by values in descending order
result = dict(sorted(scores.items(), key=lambda x: x[1], reverse=True))
# print the result
print(result)
Output
{'math': 90, 'physics': 85, 'english': 80, 'chemistry': 75}
Here We are sorting the dictionary by its values in descending order. We are passing reverse=True to sort the dictionary from higher to low values.
We can also use dictionary comprehension to sort a dictionary using its values. We will also use the sort() function along with dictionary comprehension to do it.
Code Example - ascending order
# create a dictionary
scores = {'math': 90, 'english': 80, 'physics': 85, 'chemistry': 75}
# sort the dictionary by values
result = {key: val for key, val in sorted(scores.items(), key=lambda x: x[1])}
# print the result
print(result)
Output
{'chemistry': 75, 'english': 80, 'physics': 85, 'math': 90}
Code example - Descending order
# create a dictionary
scores = {'math': 90, 'english': 80, 'physics': 85, 'chemistry': 75}
# sort the dictionary by values
result = {key: val for key, val in sorted(scores.items(), key=lambda x: x[1], reverse=True)}
# print the result
print(result)
Output
{'math': 90, 'physics': 85, 'english': 80, 'chemistry': 75}
We can use the sorted() and itemgetter() function of the operator module to sort a dictionary by values. First, we need to import the itemgetter from the operator module in our python project file. We can do that using the below code syntax.
from operator import itemgetter
After importing itemgetter function we can use it in the sorted() function as key parameter.
Code example: Ascending order
from operator import itemgetter
# create a dictionary
scores = {'math': 90, 'english': 80, 'physics': 85, 'chemistry': 75}
# sort the dictionary by values
result = dict(sorted(scores.items(), key=itemgetter(1)))
# print the result
print(result)
Output
{'chemistry': 75, 'english': 80, 'physics': 85, 'math': 90}
Code example: Descending order
from operator import itemgetter
# create a dictionary
scores = {'math': 90, 'english': 80, 'physics': 85, 'chemistry': 75}
# sort the dictionary by values using itemgetter()
result = dict(sorted(scores.items(), key=itemgetter(1), reverse=True))
# print the result
print(result)
Output
{'math': 90, 'physics': 85, 'english': 80, 'chemistry': 75}
0 Comments