**Python data structures** are available in a variety of sizes and shapes. **Python Dictionary** is one of the **data structure implementations in Python**. It’s also known as an **associative array**, and it’s a **collection of data values** that’s not in any particular order. We will discuss how to sort a dictionary by value in Python.

A **dictionary in Python** is a **collection of objects** that store data as **key-value pairs**. Every key-value pair in a dictionary corresponds to a key and its value. This procedure improves its efficiency. Values can include a list, integer, or other value, while keys can be a single element.

Dictionary items are sorted by order of insertion in Python 3.7 and later versions. They were previously unsorted or unordered collection. There are many ways to sort Python dictionaries. Let’s look at how to sort a dictionary by the values it includes.

Table of Contents

## Sorting a Dictionary with a for Loop

We can sort a dictionary with the assistance of a for loop. To begin, we utilize the sorted() function to sort the dictionary’s values. The keys for each value are then found by looping through the sorted values. We create a new dictionary with these key-value pairs in sorted order. You can’t re-order the dictionary while it’s being sorted. We’re entering the ordered pairs into a brand-new dictionary.

### Code Example

```
dict1 = {1: "One", 2: "Two", 3: "Three"}
sorted_values = sorted(dict1.values())
sorted_dict = {}
for i in sorted_values:
for k in dict1.keys():
if dict1[k] == i:
sorted_dict[k] = dict1[k]
break
print(sorted_dict)
```

### Output

So, we have seen how to sort with loops. Let’s take a look at another option: the sorted() method.

## Sorting a Dictionary with the sorted() Function

The sorted is one of the dictionary methods and a built-in function in Python. The sorted() function was originally used to sort the values of an array. When sorting a dictionary, we can add an argument to the sorted function, as shown below:

sorted(dict1, key=dict1.get).

The key is a function that is called on each element before it compares the values for sorting. The value of a dictionary’s key is returned via the get() method on dictionary objects.

The expression sorted(dict1, key=dict1.get) returns a list of keys with their values sorted in order. We can then generate a new, sorted dictionary as follows:

### Example Code

```
dict1 = {1: "One", 2: "Two", 3: "Three"}
sorted_dict = {}
sorted_keys = sorted(dict1, key=dict1.get)
for w in sorted_keys:
sorted_dict[w] = dict1[w]
print(sorted_dict)
```

### Output

While using for loops, we used the sorted() method to limit the amount of code we had to write. For a more simple manner of sorting dictionaries by values, we can combine the sorted() and itemgetter() functions.

## Using the itemgetter() method and the operator module to sort the dictionary

The operator module includes the itemgetter() function. The function returns a callable object that returns an object’s item. For this, we need to import operator module first.

Let’s imagine we develop a callable object with itemgetter() that returns the value of any dictionary with the key 2:

### Example Code

```
import operator
dict1 = {1: "One", 2: "Two", 3: "Three"}
get_item_with_key_2 = operator.itemgetter(2)
print(get_item_with_key_2(dict1))
```

### Output

The items() method is available to every dictionary. This function returns a list of tuples containing a dictionary’s key-value pairs. The 2nd value of the tuple, i.e., the value of the keys in the dictionary, can be used to sort the list of tuples using the itemgetter() method.

We can generate a dictionary based on those values once it’s been sorted:

### Example Code

```
import operator
dict = {1: "One", 2: "Two", 3: "Three"}
sorted_tuples = sorted(dict.items(), key=operator.itemgetter(1))
print(sorted_tuples)
sorted_dic = {k: v for k, v in sorted_tuples}
print(sorted_dic)
```

### Output

We now have sorted a dictionary by values with a lot less work!

We can utilize lambda functions to return dictionary values to be sorted because the key argument takes any function. Let’s see how.

## Using a Lambda Function to Sort Dictionary

In Python, lambda functions are anonymous or nameless functions. We can acquire the value of a dictionary item without needing to import the operator module for itemgetter() by using lambda functions. Those items will call as Lambda item. If you’re interested in learning more about lambdas, check out our Python Lambda Functions guide.

Let’s use a lambda function in the key argument of sorted() to sort a dictionary by values:

### Example Code

```
dict = {1: "One", 2: "Two", 3: "Three"}
sorted_tuples = sorted(dict.items(), key=lambda item: item[1])
print(sorted_tuples)
sorted_dic = {k: v for k, v in sorted_tuples}
print(sorted_dic)
```

### Output

It’s worth noting that the methods we’ve covered so far are only compatible with Python 3.7 and later. Let’s see what we can achieve with older Python versions.

## Returning a Sorted Dictionary as a New Dictionary

In Python versions before 3.7, you must use the OrderedDict – available in the collections module – to keep a sorted dictionary after sorting it by values. These are dictionaries that keep track of insertion order.

Here’s an example of utilizing OrderedDict and sorting:

### Example Code

```
import operator
from collections import OrderedDict
dict = {1: "One", 2: "Two", 3: "Three"}
sorted_tuples = sorted(dict.items(), key=operator.itemgetter(1))
print(sorted_tuples)
sorted_dict = OrderedDict()
for k, v in sorted_tuples:
sorted_dict[k] = v
print(sorted_dict)
```

### Output

## Conclusion

In conclusion, we discussed sorting a dictionary by values in Python. We saw different ways to sort a dictionary by value in Python, like using sorted() method, with operator module, using lambda, itemgetter() method, getting a new dictionary after sorting it, and using the loop. Also, we performed some coding examples to get clear knowledge.