How Do You Use a Nested Dictionary in Python?

Problem scenario
You want to create a dictionary of dictionaries in Python. You want to use them. What do you do?

Solution
Here is an example/illustration:

contint_dict = {}
contint_dict["a_test"] = "123"
contint_dict["b_test"] = "456"
contint_dict["c_test"] = "789"

color_animal_dict = {}
color_animal_dict["blue"] = "dog"
color_animal_dict["orange"] = "cat"
color_animal_dict["green"] = "goat"

days_dict = {}
days_dict["Tuesday"] = "midnight"
days_dict["Wednesday"] = "noon"
days_dict["Thursday"] = "evening"

big_dictionary = {}
big_dictionary["dict_1"] = contint_dict
big_dictionary["dict_2"] = color_animal_dict
big_dictionary["dict_3"] = days_dict

print(big_dictionary["dict_1"]["b_test"])
print(big_dictionary["dict_2"]["green"])
print(big_dictionary["dict_3"]["Wednesday"])

super_dictionary = {}
super_dictionary["top_of_nested"] = big_dictionary
print(super_dictionary["top_of_nested"]["dict_3"]["Wednesday"])

You may also want to see this: https://pypi.org/project/nested_dict/

Leave a comment

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