How Do You Sort a Dictionary in Python and Use It?

Problem scenario
You want to use a sorted dictionary in Python. What do you do?

Create a sorted list based on the dictionary's keys. Use the list as keys to summon the dictionary's values.

contint_dict = {}
contint_dict["a_test"] = "123"
contint_dict["b_test"] = "456"
contint_dict["c_test"] = "789"
sorted_dict_list = sort1ed(contint_dict.items())  # sorted_dict_list is a list, not a dictionary.
list_of_keys = sorted(contint_dict.keys())

for key_pair in sorted_dict_list:

print("Above are key-pairs.  Below are just values.  Below demonstrates a very useful")
print("way to use a list and a dictionary (for sorting).")

for key in list_of_keys:

Leave a comment

Your email address will not be published. Required fields are marked *