python

# Sort a dictionary by value in Python

You have a dictionary that contains values in no particular order. You just want the dictionary sorted by value. In Python, dictionaries are unordered, like lists. We could sort the dictionary by key, but sometimes the key is unknown or not meaningful.

``````# 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}``

## Method 1: Sort a dictionary by value using sorted() and lambda functions

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,

1. We have created dictionary - scores that contain multiple key-value pairs in it.
2. In the sorted() function we have passed scores.items() as the first parameter and in the key parameter, we are passing lambda x: x[1].
3. We are converting the output to a dictionary using the dict() function and assigning it to a variable named result.
4. Print the result.

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.

## Method 2: Sort Dictionary by value using comprehension and sorted() function

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}``

## Method 3: Using operator.itemgetter and sorted() function

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}``