Wednesday, 24 June 2026

πŸš€ Day 74/150 – Sort Dictionary by Keys in Python

 



πŸš€ Day 74/150 – Sort Dictionary by Keys in Python

Dictionaries are one of the most commonly used data structures in Python. Sometimes, you may want to display or process dictionary data in a sorted order based on its keys. Python provides several simple ways to achieve this.

Let's explore four different methods to sort a dictionary by its keys.

πŸ”Ή Method 1 – Using sorted()

The sorted() function returns the dictionary keys in alphabetical order.

student = { "name": "John", "age": 20, "course": "Python" } for key in sorted(student): print(key, ":", student[key])




Output

age : 20
course : Python
name : John

Explanation

  • sorted(student) sorts all keys alphabetically.
  • We then access each value using student[key].

πŸ”Ή Method 2 – Creating a Sorted Dictionary

You can create an entirely new dictionary with keys already sorted.

student = { "name": "John", "age": 20, "course": "Python" } sorted_dict = {key: student[key] for key in sorted(student)} print(sorted_dict)





Output

{'age': 20, 'course': 'Python', 'name': 'John'}

Explanation
  • Dictionary comprehension creates a new dictionary.
  • Keys are inserted in sorted order.

πŸ”Ή Method 3 – Using dict(sorted())

A concise and commonly used approach.

student = { "name": "John", "age": 20, "course": "Python" } sorted_dict = dict(sorted(student.items())) print(sorted_dict)





Output

{'age': 20, 'course': 'Python', 'name': 'John'}

Explanation
  • items() returns key-value pairs.
  • sorted() sorts those pairs by key.
  • dict() converts the sorted list back into a dictionary.

πŸ”Ή Method 4 – Using Another Dictionary

Works for any dictionary data.

data = { "banana": 3, "apple": 5, "mango": 2 } sorted_data = dict(sorted(data.items())) print(sorted_data)





Output

{'apple': 5, 'banana': 3, 'mango': 2}

Explanation
  • Keys are sorted alphabetically.
  • Useful when working with real-world datasets.

🎯 When to Use Which Method?

MethodBest Use Case
sorted()Display keys in sorted order
Dictionary ComprehensionCreate a new sorted dictionary
dict(sorted())Clean and concise solution
User Dictionary ExamplePractical real-world sorting

πŸ’‘ Pro Tip

To sort a dictionary in reverse alphabetical order, use:

sorted_dict = dict(sorted(data.items(), reverse=True))

Output
{'mango': 2, 'banana': 3, 'apple': 5}

✅ Sorting dictionaries by keys is a useful skill when displaying reports, organizing data, or preparing outputs for users. Python's built-in sorted() function makes the task simple and efficient.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (291) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (30) Azure (11) BI (10) Books (262) Bootcamp (11) C (78) C# (12) C++ (83) cloud (1) Course (87) Coursera (300) Cybersecurity (32) data (6) Data Analysis (37) Data Analytics (25) data management (16) Data Science (374) Data Strucures (22) Deep Learning (183) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (21) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (74) Git (12) Google (53) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (42) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (325) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (14) PHP (20) Projects (34) Python (1387) Python Coding Challenge (1169) Python Mathematics (1) Python Mistakes (51) Python Quiz (550) Python Tips (17) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (20) SQL (52) Udemy (18) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)