Skip to content
Home / Fundamentals

Sorting a Dictionary by Value in Python

In Python, a dictionary is an unordered collection of key-value pairs. While dictionaries do not have a built-in way to be sorted, there are several techniques that can be used to sort a dictionary by value.

Method 1: Using the sorted Function

One way to sort a dictionary by value is to use the built-in sorted function. The sorted function takes an iterable and returns a sorted list of items.

To sort a dictionary by value, you can pass the dictionary to the sorted function and specify the value parameter as the sorting key. For example:

d = {'apple': 2, 'banana': 1, 'cherry': 3}
sorted_d = sorted(d.items(), key=lambda x: x[1])
print(sorted_d)

This will output the following sorted list of tuples:

[('banana', 1), ('apple', 2), ('cherry', 3)]

You can then convert the sorted list of tuples back into a dictionary using the dict function:

sorted_dict = dict(sorted_d)
print(sorted_dict)

This will output the following dictionary:

{'banana': 1, 'apple': 2, 'cherry': 3}

Method 2: Using the OrderedDict Class

Another way to sort a dictionary by value is to use the OrderedDict class from the collections module. The OrderedDict class is a subclass of the built-in dict class that allows you to specify the order of the keys in the dictionary.

To sort a dictionary by value using an OrderedDict, you can pass the dictionary to the OrderedDict constructor and then use the sorted function to sort the resulting OrderedDict by value.

For example:

from collections import OrderedDict

d = {'apple': 2, 'banana': 1, 'cherry': 3}
sorted_dict = OrderedDict(sorted(d.items(), key=lambda x: x[1]))
print(sorted_dict)

This will output the following OrderedDict:

OrderedDict([('banana', 1), ('apple', 2), ('cherry', 3)])

Method 3: Using the sort Method of a dict View

Another way to sort a dictionary by value is to use the sort method of a dictionary view object. A dictionary view object is a dynamic view of the dictionary's keys, values, or items that allows you to perform operations on the dictionary without creating a new object.

d = {'apple': 2, 'banana': 1, 'cherry': 3}
sorted_dict = dict(sorted(d.items(), key=lambda x: x[1]))
print(sorted_dict)

This will output the following dictionary:

{'banana': 1, 'apple': 2, 'cherry': 3}

Method 4: Using the sorted Function and a lambda Function

Another way to sort a dictionary by value is to use the sorted function and a lambda function. A lambda function is a small anonymous function that can be used to specify a function as an argument.

To sort a dictionary by value using a lambda function, you can pass the dictionary to the sorted function and specify a lambda function as the sorting key. The lambda function should take a dictionary entry as input and return the value of the entry.

For example:

d = {'apple': 2, 'banana': 1, 'cherry': 3}
sorted_dict = dict(sorted(d.items(), key=lambda x: x[1]))
print(sorted_dict)

This will output the following dictionary:

{'banana': 1, 'apple': 2, 'cherry': 3}