---
title: "How do you sort a dictionary by its values in Python?"  
description: "How do you sort a dictionary by its values in Python?"  
author: "Utpal Vishwas"  
published: 2023-06-26  
updated: 2023-06-27  
canonical: https://www.mindstick.com/forum/158872/how-do-you-sort-a-dictionary-by-its-values-in-python  
category: "python"  
tags: ["dictionary", "python"]  
reading_time: 2 minutes  

---

# How do you sort a dictionary by its values in Python?

How do you sort a [dictionary](https://www.mindstick.com/articles/1500/hashtable-and-dictionary-in-c-sharp) by its [values](https://www.mindstick.com/forum/327/sum-textbox-values) in [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python)?

## Replies

### Reply by Aryan Kumar

There are a few ways to sort a dictionary by its values in Python. Here are two of the most common methods:

**Using the** `sorted()` **function**

The `sorted()` function can be used to sort any iterable object, including dictionaries. To sort a dictionary by its values, you would simply pass the dictionary to the `sorted()` function and specify the `key` argument. The `key` argument takes a function as its input, and the function will be used to determine the order of the elements in the iterable. In this case, you would pass a lambda function to the `key` argument. The lambda function would simply return the value of each key-value pair in the dictionary.

For example, the following code sorts a dictionary by its values:

Python

```plaintext
def sort_by_value(dict1):
    sorted_dict = sorted(dict1.items(), key=lambda x: x[1])
    return sorted_dict

dict1 = {'a': 10, 'b': 5, 'c': 20, 'd': 1}
sorted_dict = sort_by_value(dict1)
print(sorted_dict)
```

This code will print the following output:

Code snippet

```plaintext
[('c', 20), ('a', 10), ('d', 1), ('b', 5)]
```

**Using the** `items()` **method**

The `items()` method returns a list of key-value pairs from a dictionary. You can then use the `sorted()` function to sort the list of key-value pairs by their values.

For example, the following code sorts a dictionary by its values using the `items()` method:

Python

```plaintext
def sort_by_value(dict1):
    sorted_dict = sorted(dict1.items(), key=lambda x: x[1])
    return sorted_dict

dict1 = {'a': 10, 'b': 5, 'c': 20, 'd': 1}
sorted_dict = sort_by_value(dict1)
print(sorted_dict)
```

This code will print the same output as the previous example.

Which method you use to sort a dictionary by its values will depend on your specific needs. If you need to sort the dictionary in place, then you will need to use the `sorted()` function with the `key` argument. If you do not need to sort the dictionary in place, then you can use the `items()` method and the `sorted()` function.


---

Original Source: https://www.mindstick.com/forum/158872/how-do-you-sort-a-dictionary-by-its-values-in-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
