Introduction
Sorting a dictionary by its keys is a common task when you need to organize the data in a specific order. In Python, dictionaries are unordered by default, but you can sort them based on their keys using various methods. This tutorial will guide you through creating a Python program that sorts a dictionary by its keys.
Example:
-
Input Dictionary:
{'b': 3, 'a': 1, 'c': 2} -
Output Dictionary:
{'a': 1, 'b': 3, 'c': 2} -
Input Dictionary:
{'banana': 3, 'apple': 1, 'cherry': 2} -
Output Dictionary:
{'apple': 1, 'banana': 3, 'cherry': 2}
Problem Statement
Create a Python program that:
- Takes a dictionary as input.
- Sorts the dictionary by its keys.
- Displays the sorted dictionary.
Solution Steps
- Create a Dictionary: Initialize a dictionary with some key-value pairs.
- Sort the Dictionary by Keys: Use the
sorted()function to sort the dictionary by keys. - Display the Sorted Dictionary: Use the
print()function to display the sorted dictionary.
Python Program
# Python Program to Sort a Dictionary by Key
# 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 keys using the sorted() function
sorted_dict = dict(sorted(my_dict.items()))
# Step 3: Display the sorted dictionary
print("The sorted dictionary by key is:", sorted_dict)
Explanation
Step 1: Create a Dictionary with Key-Value Pairs
- A dictionary
my_dictis created with some key-value pairs, such as'b': 3,'a': 1,'c': 2, and'd': 4.
Step 2: Sort the Dictionary by Keys Using the sorted() Function
- The
sorted()function is used to sort the items of the dictionary by keys. Theitems()method is called on the dictionary to get a view object that displays a list of the dictionary’s key-value tuple pairs. - The sorted list of tuples is then converted back into a dictionary using the
dict()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 key is: {'a': 1, 'b': 3, 'c': 2, 'd': 4}
Example 2:
# Python Program to Sort a Dictionary by Key
# Author: https://www.rameshfadatare.com/
# Step 1: Create a dictionary with key-value pairs
my_dict = {
'banana': 3,
'apple': 1,
'cherry': 2,
'date': 5
}
# Step 2: Sort the dictionary by keys using the sorted() function
sorted_dict = dict(sorted(my_dict.items()))
# Step 3: Display the sorted dictionary
print("The sorted dictionary by key is:", sorted_dict)
Output:
The sorted dictionary by key is: {'apple': 1, 'banana': 3, 'cherry': 2, 'date': 5}
Example 3:
# Python Program to Sort a Dictionary by Key
# Author: https://www.rameshfadatare.com/
# Step 1: Create a dictionary with key-value pairs
my_dict = {
'zebra': 5,
'ant': 1,
'elephant': 7,
'cat': 3
}
# Step 2: Sort the dictionary by keys using the sorted() function
sorted_dict = dict(sorted(my_dict.items()))
# Step 3: Display the sorted dictionary
print("The sorted dictionary by key is:", sorted_dict)
Output:
The sorted dictionary by key is: {'ant': 1, 'cat': 3, 'elephant': 7, 'zebra': 5}
Conclusion
This Python program demonstrates how to sort a dictionary by its keys using the sorted() function. This method is efficient and straightforward, allowing you to organize data in a dictionary based on the order of the keys. Understanding how to sort dictionaries is essential for tasks involving data organization and retrieval in Python.