Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion Dictionary-Sort a Dictionary by Keys and Values.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,25 @@ To write a Python program that sorts a dictionary's:
---

## 🧪Program
Add Code here
```
data = {'banana': 'yellow', 'cherry': 'red', 'apple': 'green', 'blueberry': 'blue'}

sorted_by_keys = dict(sorted(data.items()))
sorted_by_values = dict(sorted(data.items(), key=lambda item: item[1]))

print("Original dictionary:", data)
print("Dictionary sorted by keys:", sorted_by_keys)
print("Dictionary sorted by values:", sorted_by_values)
```

## Sample Output
```
Original dictionary: {'banana': 'yellow', 'cherry': 'red', 'apple': 'green', 'blueberry': 'blue'}
Dictionary sorted by keys: {'apple': 'green', 'banana': 'yellow', 'blueberry': 'blue', 'cherry': 'red'}
Dictionary sorted by values: {'blueberry': 'blue', 'apple': 'green', 'cherry': 'red', 'banana': 'yellow'}
```

## Result

The program successfully sorts the dictionary by keys and values in alphabetical order and prints the sorted results.