AppDividend
Latest Code Tutorials

# How to Sort Dictionary By Value in Python 3.7

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

## Sort Dictionary By Value in Python

To sort the 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)}

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, 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. 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 Python 3.7 version, after sorting a dictionary by values, to keep a sorted dictionary, we have to use the OrderedDict – available in the collections module. 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 method is short and it will give you exactly what you want. If you want in descending order, pass reverse=True to the sorted() method.

That is it for this tutorial.

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