diff --git a/Dictionary-Sort a Dictionary by Keys and Values.md b/Dictionary-Sort a Dictionary by Keys and Values.md index d58029db..a1ed0a0c 100644 --- a/Dictionary-Sort a Dictionary by Keys and Values.md +++ b/Dictionary-Sort a Dictionary by Keys and Values.md @@ -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. +