🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Sorting a dictionary by its keys is a common task in Python. It allows you to organize and represent dictionary data in a specific order. Python dictionaries are inherently unordered (though as of Python 3.7, insertion order is preserved), but there are ways to sort them for display or further processing.
Definition
Sorting a dictionary by keys means arranging the dictionary entries based on the order of the keys using some sorting criteria, typically in ascending or descending order.
2. Program Steps
1. Create a dictionary with unordered keys.
2. Use the sorted() function to sort the dictionary keys.
3. Create a new dictionary that is ordered by the sorted keys.
4. Print the sorted dictionary.
3. Code Program
# Create an unordered dictionary
my_dict = {'banana': 3, 'apple': 2, 'pear': 5, 'orange': 1}
# Sort the dictionary by keys
sorted_keys = sorted(my_dict.keys())
# Create a new dictionary with sorted keys
sorted_dict = {key: my_dict[key] for key in sorted_keys}
# Print the sorted dictionary
print(f"Dictionary sorted by keys: {sorted_dict}")
Output:
Dictionary sorted by keys: {'apple': 2, 'banana': 3, 'orange': 1, 'pear': 5}
Explanation:
1. my_dict is a dictionary with fruit names as keys and integers as values. The keys are in an unordered state.
2. sorted_keys uses the sorted() function to sort the keys of my_dict. The sorted() function returns a new list containing all keys in sorted order.
3. sorted_dict is a dictionary comprehension that iterates over sorted_keys, creating a new dictionary with the same keys in sorted order.
4. Each key from sorted_keys is used to get the corresponding value from my_dict, ensuring that the key-value pairs remain intact.
5. The print statement displays sorted_dict, showing the dictionary sorted by keys. The output confirms that the keys are now sorted alphabetically.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment