# How to Sort Dictionary By Value in Python

In previous versions of Python, the dictionaries are inherently orderless, but other data types, such as lists and tuples, are not orderless. As for Python 3.6, the built-in dictionary will be ordered. We have already seen how to sort a dictionary by key in Python.

## How to sort a dictionary by value in Python

To sort a dictionary by value in Python, use the combination of dictionary comprehension, sorted() method, for loop, dict.items(), and lambda function.

Dictionaries can’t be sorted, but you can build a sorted list from them. To get the list, use the dict.items() function.

### Example

```data = {1: 21, 3: 46, 4: 31, 2: 19, 0: 11}

sorted_by_val = {k: b for k, b in sorted(
data.items(), key=lambda element: element[1])}

print(sorted_by_val)```

#### Output

`{0: 11, 2: 19, 1: 21, 4: 31, 3: 46}`

This code will sort the dictionary by the values of each entry within the dictionary, from smallest to largest.

You can see from the output that the dictionary is sorted by its values.

To sort it in descending order, add reverse=True.

```data = {1: 21, 3: 46, 4: 31, 2: 19, 0: 11}

sorted_by_val = {k: b for k, b in sorted(
data.items(), key=lambda element: element[1], reverse=True)}

print(sorted_by_val)```

#### Output

`{3: 46, 4: 31, 1: 21, 2: 19, 0: 11}`

## Sort Dictionary by value using a for loop

To sort a dictionary by value, use the sorted() function to order the dictionary’s values and then use the for loop through the sorted values, finding the keys for each value. Finally, we add these key-value pairs in the sorted order into a new dictionary.

```data = {1: 21, 3: 46, 4: 31, 2: 19, 0: 11}

sorted_values = sorted(data.values()) # Sort the values
sorted_dict = {}

for i in sorted_values:
for k in data.keys():
if data[k] == i:
sorted_dict[k] = data[k]
break

print(sorted_dict)```

#### Output

`{0: 11, 2: 19, 1: 21, 4: 31, 3: 46}`

## Before Python 3.7, Sort Dictionary by values using the sorted()

Before the Python 3.7 version, after sorting a dictionary by values, we had to use the OrderedDict – available in the collections module to keep a sorted dictionary. These objects are dictionaries that keep the order of insertion.

```import operator
from collections import OrderedDict

data = {1: 21, 3: 46, 4: 31, 2: 19, 0: 11}

sorted_tuples = sorted(data.items(), key=operator.itemgetter(1))
sorted_dict = OrderedDict()

for k, v in sorted_tuples:
sorted_dict[k] = v

print(sorted_dict)```

#### Output

`OrderedDict([(0, 11), (2, 19), (1, 21), (4, 31), (3, 46)])`

And you can see that we got the dictionary sorted by values using the OrderedDict from the collections module.

## Conclusion

In this tutorial, we have seen how to sort a dictionary based on its values. We first sorted a dictionary using for loop, sorted, lambda function, and dictionary comprehension.

This short method will give you precisely what you want if you wish to pass reverse=True to the sorted() method in descending order.

That is it for this tutorial.

This site uses Akismet to reduce spam. Learn how your comment data is processed.