You are currently viewing How to Sort Dictionary by Value in Python

How to sort a dictionary by value in Python? In Python, you can sort a dictionary by its values using the sorted() function along with a custom sorting key. It can be used to sort a dictionary by value in ascending order or descending order. This function sorts iterable objects like lists, tuples, sets, and dictionaries. In this article, I will explain how to sort a dictionary by value in ascending/descending order using python sorted() function.

Advertisements

1. Quick Examples of Sort Dictionary by Value

If you are in a hurry, below are some quick examples of sorting dictionaries by value in Python.


# Below are the quick examples

# Example 1:  Sort the dictionary by value in ascending order
sorted_list = sorted(my_dict.items(), key = lambda x:x[1])

# Example 2:  Sort the dictionary by value in descending order 
sorted_list = sorted(my_dict.items(), key = lambda x:x[1], reverse = True)

# Example 3: Update the existing dictionary with sorted values
sorted_list = sorted(my_dict.items(), key = lambda x:x[1]
my_dict.clear()
for key, value in sorted_list:
    my_dict[key] = value

# Example 4: Sort the dictionary values
print(sorted(my_dict.values()))

2. What is Python Dictionary

A Python dictionary is a collection that is unordered, mutable, and does not allow duplicates. Each element in the dictionary is in the form of key:value pairs. Dictionary elements should be enclosed with {} and key:value pair separated by commas. The dictionaries are indexed by keys.

Let’s create a Python dictionary where, the 'keys' are 'string' type and 'values' are 'int' type.


# Create dictionary
my_dict = {'Apple':5, 'papaya':6, 'kiwi':4, 'pomegranate':11, 'strawberry':10}
print(my_dict)
print(type(my_dict))

Yields below output.

Sort Dictionary Value Python

3. Sort Python Dictionary by Value in Ascending Order

Using the sorted() method we can sort the dictionary by value in Python. Pass the items() function as an iterable and key param(specify to sort by values using lambda function) into this function, it will sort the dictionary by values and finally, get the sorted dictionary by values using the dict() function.


# Sort the dictionary by value in ascending order
sorted_list = sorted(my_dict.items(), key = lambda x:x[1])
print("Sorting dictionary in ascending order:\n",sorted_list)

Yields below output.

Sort Dictionary Value Python

4. Sort Dictionary by Value in Descending Order

To sort a Python dictionary by its values in descending order, you can use the sorted() function with the reverse=True parameter.


# Sort the dictionary by value in descending order 
sorted_list = sorted(my_dict.items(), key = lambda x:x[1], reverse = True)
print("Sorting dictionary in descending order:\n",sorted_list)

# Output:
# Sorting dictionary in descending order:
#  [('pomegranate', 11), ('strawberry', 10), ('papaya', 6), ('Apple', 5), ('kiwi', 4)]

5. Update the Existing Dictionary with Sorted Values in Python

When we sort the dictionary by using the sorted() function it will return a list of tuple values, to update the original dictionary with a sorted key-value pair we can use the dict.items() function. Using this we can get a list of values and sort them, and then use a for loop to update the dictionary with sorted key-value pair.


# Update the existing dictionary with sorted values
sorted_list = sorted(my_dict.items(), key = lambda x:x[1]
my_dict.clear()
for key, value in sorted_list:
    my_dict[key] = value
print(my_dict)

# Output:
# {'kiwi': 4, 'Apple': 5, 'papaya': 6, 'strawberry': 10, 'pomegranate': 11}

6. Get Only Values After Sorting by Value

Let’s pass dict.values() function into sorted() function, it will return a list of all sorted values stored in the dictionary. For example,


# Sort the dictionary values
sorted_values = sorted(my_dict.values())
print("Sorting dictionary values:\n",sorted_values)

# Output:
# Sorting dictionary values:
#  [4, 5, 6, 10, 11]

Frequently Asked Questions on Sort Dictionary by Value in Python

How can I sort a dictionary by its values in ascending order?

To sort a dictionary by its values in ascending order, you can use the sorted() function along with a lambda function as the sorting key. For example, sorted() is used with the items() method of the dictionary to get a list of key-value pairs. The key parameter is set to a lambda function that extracts the second element of each pair (the values). This lambda function is used as the sorting key, so the dictionary is sorted based on its values.

How do I sort a dictionary by values in descending order?

To sort a dictionary by its values in descending order, you can use the sorted() function along with a lambda function as the sorting key and setting the reverse parameter to True.

Can I get only the sorted values of the dictionary without the keys?

You can obtain only the sorted values of the dictionary without the keys. For example, the sorted() function is used to obtain a list of key-value pairs sorted by values. Then, a list comprehension is used to extract and store only the values in the sorted_values list.

How to sort a dictionary and get both keys and values in separate lists?

To sort a dictionary and get both keys and values in separate lists, you can use the zip() function along with the sorted() function

Can I sort a dictionary in place by its values?

The built-in sorted() function returns a new sorted list and does not modify the original dictionary. However, you can sort a dictionary in place using the sort() method of the dictionary’s items.

Conclusion

In this article, I have explained how to sort a dictionary by value in ascending/descending order using the Python sorted() function. And also explained how we can update the existing dictionary with sorted key-value pairs.

Related Articles

References