Sort dictionary by values Dry Run in PYTHON

Sort dictionary by values is an interactive PYTHON dry run visualizer from the Dict programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Sort dictionary by values Program Code

# Sort dictionary by values
scores = {"Physics": 76, "Math": 94, "Chemistry": 85, "Biology": 91, "English": 88}
print("Original:", scores)

# Method 1: Sort by values ascending
sorted_by_values_asc = dict(sorted(scores.items(), key=lambda x: x[1]))
print("By values (ascending):", sorted_by_values_asc)

# Method 2: Sort by values descending
sorted_by_values_desc = dict(sorted(scores.items(), key=lambda x: x[1], reverse=True))
print("By values (descending):", sorted_by_values_desc)

# Method 3: Get top 3 highest scores
top_3 = dict(sorted(scores.items(), key=lambda x: x[1], reverse=True)[:3])
print("Top 3 scores:", top_3)

# Method 4: Get lowest 2 scores
lowest_2 = dict(sorted(scores.items(), key=lambda x: x[1])[:2])
print("Lowest 2 scores:", lowest_2)

View the complete Dict programs page.

Program Console Sort dictionary by values Topic: PYTHON Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.