Introduction
Sorting a dictionary by its values is a common task when you need to organize data based on the magnitude of the values rather than the keys. This tutorial will guide you through creating a Python program that sorts a dictionary by its values.
Example:
-
Input Dictionary:
{'b': 3, 'a': 1, 'c': 2}
-
Output Dictionary (sorted by value):
{'a': 1, 'c': 2, 'b': 3}
-
Input Dictionary:
{'banana': 3, 'apple': 1, 'cherry': 2}
-
Output Dictionary (sorted by value):
{'apple': 1, 'cherry': 2, 'banana': 3}
Problem Statement
Create a Python program that:
- Takes a dictionary as input.
- Sorts the dictionary by its values.
- Displays the sorted dictionary.
Solution Steps
- Create a Dictionary: Initialize a dictionary with some key-value pairs.
- Sort the Dictionary by Values: Use the
sorted()
function with a custom key to sort the dictionary by values. - Display the Sorted Dictionary: Use the
print()
function to display the sorted dictionary.
Python Program
# Python Program to Sort a Dictionary by Value
# Author: https://www.rameshfadatare.com/
# Step 1: Create a dictionary with key-value pairs
my_dict = {
'b': 3,
'a': 1,
'c': 2,
'd': 4
}
# Step 2: Sort the dictionary by values using the sorted() function and lambda
sorted_dict = dict(sorted(my_dict.items(), key=lambda item: item[1]))
# Step 3: Display the sorted dictionary
print("The sorted dictionary by value is:", sorted_dict)
Explanation
Step 1: Create a Dictionary with Key-Value Pairs
- A dictionary
my_dict
is created with some key-value pairs, such as'b': 3
,'a': 1
,'c': 2
, and'd': 4
.
Step 2: Sort the Dictionary by Values Using the sorted() Function and lambda
- The
sorted()
function is used to sort the items of the dictionary by values. Thekey=lambda item: item[1]
argument tells thesorted()
function to sort the dictionary based on the second element of the tuple (i.e., the value). The sorted list of tuples is then converted back into a dictionary using thedict()
constructor.
Step 3: Display the Sorted Dictionary
- The
print()
function is used to display the sorted dictionary.
Output Example
Example 1:
The sorted dictionary by value is: {'a': 1, 'c': 2, 'b': 3, 'd': 4}
Example 2:
# Python Program to Sort a Dictionary by Value
# Author: https://www.rameshfadatare.com/
# Step 1: Create a dictionary with key-value pairs
my_dict = {
'banana': 3,
'apple': 1,
'cherry': 2,
'date': 4
}
# Step 2: Sort the dictionary by values using the sorted() function and lambda
sorted_dict = dict(sorted(my_dict.items(), key=lambda item: item[1]))
# Step 3: Display the sorted dictionary
print("The sorted dictionary by value is:", sorted_dict)
Output:
The sorted dictionary by value is: {'apple': 1, 'cherry': 2, 'banana': 3, 'date': 4}
Example 3:
# Python Program to Sort a Dictionary by Value
# Author: https://www.rameshfadatare.com/
# Step 1: Create a dictionary with key-value pairs
my_dict = {
'zebra': 50,
'ant': 10,
'elephant': 70,
'cat': 30
}
# Step 2: Sort the dictionary by values using the sorted() function and lambda
sorted_dict = dict(sorted(my_dict.items(), key=lambda item: item[1]))
# Step 3: Display the sorted dictionary
print("The sorted dictionary by value is:", sorted_dict)
Output:
The sorted dictionary by value is: {'ant': 10, 'cat': 30, 'zebra': 50, 'elephant': 70}
Conclusion
This Python program demonstrates how to sort a dictionary by its values using the sorted()
function and a lambda function as the key. This method is efficient and straightforward, allowing you to organize data in a dictionary based on the order of the values. Understanding how to sort dictionaries by value is essential for tasks involving data analysis and organization in Python.