Thursday, 25 June 2026

πŸš€ Day 75/150 – Sort Dictionary by Values in Python

 


πŸš€ Day 75/150 – Sort Dictionary by Values in Python

Dictionaries often store important data such as marks, prices, salaries, or scores. Sometimes, instead of sorting by keys, you may want to sort the dictionary based on its values.

Let's explore different ways to sort a dictionary by values in Python.


πŸ”Ή Method 1 – Using sorted() with lambda

The most common approach is to use sorted() along with a lambda function.

student = { "John": 85, "Alice": 92, "Bob": 78 } sorted_data = dict( sorted(student.items(), key=lambda item: item[1]) ) print(sorted_data)





Output

{'Bob': 78, 'John': 85, 'Alice': 92}

Explanation
  • items() returns key-value pairs.
  • item[1] refers to the value.
  • sorted() arranges pairs according to values.

πŸ”Ή Method 2 – Sorting in Descending Order

To sort from highest to lowest value:

student = { "John": 85, "Alice": 92, "Bob": 78 } sorted_data = dict( sorted(student.items(), key=lambda item: item[1], reverse=True) ) print(sorted_data)






Output

{'Alice': 92, 'John': 85, 'Bob': 78}

Explanation
  • reverse=True sorts values in descending order.

πŸ”Ή Method 3 – Using Function

def sort_by_values(data): return dict( sorted(data.items(), key=lambda item: item[1]) ) marks = { "Math": 90, "English": 80, "Science": 95 } print(sort_by_values(marks))













Output
{'English': 80, 'Math': 90, 'Science': 95}

Explanation
  • Encapsulates sorting logic inside a reusable function.

πŸ”Ή Method 4 – Taking User Dictionary

data = { "apple": 50, "banana": 20, "mango": 35 } sorted_data = dict( sorted(data.items(), key=lambda item: item[1]) ) print(sorted_data)












Output
{'banana': 20, 'mango': 35, 'apple': 50}

Explanation
  • Useful for sorting product prices, quantities, scores, etc.

🎯 Real-World Uses

✅ Ranking students by marks

✅ Sorting products by price

✅ Displaying leaderboard scores

✅ Organizing sales reports

✅ Analyzing frequency counts


πŸ’‘ Pro Tip

To get the highest-value item:

student = {
"John": 85,
"Alice": 92,
"Bob": 78
}

highest = max(student.items(), key=lambda item: item[1])

print(highest)

Output

('Alice', 92)


πŸ”₯ Key Takeaways

✔️ Use sorted(dictionary.items(), key=lambda item: item[1]) to sort by values.

✔️ item[1] refers to dictionary values.

✔️ reverse=True sorts in descending order.

✔️ dict() converts sorted pairs back into a dictionary.

✔️ Sorting by values is common in ranking and reporting applications.

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)