Sort a Dictionary in the ascending order of Values Python

In this tutorial, we will learn various approaches to sort a Dictionary in the ascending order of Values Python.

A dictionary can be sorted by values in the ascending order using sorted method in Python.

We will look at the various approaches to sort a dictionary by values in ascending order in Python.

Approach 1: Sort a dictionary in the ascending order of values using sorted(), items() and for loop in Python

In this approach, we will do the following to sort a dictionary in the ascending order of values using sorted(), items() and for loop.

d1 = {'Bob': 60, 'Alice': 43, 'Ram': 80, 'Geeta': 49, 'Shreyan': 95, 'Raj': 68}
sorted_by_values = dict()
tup = list()
for k, v in d1.items():
    tup.append((v, k))

for v, k in sorted(tup):
    sorted_by_values[k] = v

print('Before sort')
print(d1)
print('After sorting a dictionary by its value in ascending order')
print(sorted_by_values)
Sort a dictionary in the ascending order of values using sorted(), items() and for loop in Python

In the preceding code, we perform the following steps in the order described below.

  • First we define a resultant dictionary that will hold the key value pairs whose order will be in the ascending order of values.
  • We then define a list in which we will maintain tuples of key value pair of dictionary
  • We then iterate over dictionary using items() and add value and then key pair as tuples to the list.
  • We then perform sort over the list of tuples having value and key pairs.
  • We finally iterate over the sorted list of tuples and add the value key pair to the resultant dictionary by mapping second item in the tuple as key and first item in the tuple as value.

The output of the preceding code will be as follows.

Before sort
{'Bob': 60, 'Alice': 43, 'Ram': 80, 'Geeta': 49, 'Shreyan': 95, 'Raj': 68}
After sorting a dictionary by its value in ascending order
{'Alice': 43, 'Geeta': 49, 'Bob': 60, 'Raj': 68, 'Ram': 80, 'Shreyan': 95}
Output: Sort a dictionary in the ascending order of values using sorted(), items() and for loop in Python

We notice that the resultant dictionary has the values in the ascending order with their respective values. We also notice that after sorting a dictionary by value, the results shows the students in the ascending order of marks.

Approach 2: Sort a dictionary in the ascending order of values using sorted(), lambda and dictionary comprehension in Python

In this approach, we will do the following to sort a dictionary in the ascending order of values using sorted(), items() and dictionary comprehension.

d1 = {'Bob': 60, 'Alice': 43, 'Ram': 80, 'Geeta': 49, 'Shreyan': 95, 'Raj': 68}
sorted_by_values = {k: v for k, v in sorted(d1.items(), key=lambda item: item[1])}

print('Before sort')
print(d1)
print('After sorting a dictionary by its value in ascending order using dictionary comprehension')
print(sorted_by_values)
Sort a dictionary in the ascending order of values using sorted(), lambda and dictionary comprehension in Python

Here, in the preceding code, we are sorting a dictionary by its ascending order of values in Python using dictionary comprehension, sorted() and lambda.

We notice that the sorted method is provided with two arguments,

  • The first argument is the list of tuples represented by d1.items().
  • Each item in the list of tuple let's say is represented by variable name item. Then the item is of type tuple and each element in a tuple is accessed by it's index position. In case of key and value being the items of item tuple, the element at index 0, represented by item[0] is key and the element at index 1 represented by item[1] is the value.
  • Hence, the second argument to the sorted method, key is specifying to the sorted method to perform the sort operation based on the item[1] which is the value amongst  the key value pair.

The output of the preceding code will be as follows.

Before sort
{'Bob': 60, 'Alice': 43, 'Ram': 80, 'Geeta': 49, 'Shreyan': 95, 'Raj': 68}
After sorting a dictionary by its value in ascending order using dictionary comprehension
{'Alice': 43, 'Geeta': 49, 'Bob': 60, 'Raj': 68, 'Ram': 80, 'Shreyan': 95}
Output: Sort a dictionary in the ascending order of values using sorted(), lambda and dictionary comprehension in Python

We notice that the resultant dictionary has the values in the ascending order with their respective values. We also notice that after sorting a dictionary by value, the results shows the students in the ascending order of marks.