Python Dictionary of Dictionaries

How to Use Python Dictionary of Dictionaries

A dictionary of dictionaries is a nested dictionary that contains dictionaries as its values. Each key in the outer dictionary points to another dictionary, which can contain its own set of keys and values. This data structure is also known as a nested dictionary, and it can be used to represent complex data sets that have a hierarchical structure.

How to Create a Dictionary of Dictionaries in Python

To create a dictionary of dictionaries in Python, you can use the following syntax:

dict_of_dicts = {'dict1': {'key1': 'value1'}, 'dict2': {'key2': 'value2'}}

In this example, we have created a dictionary of dictionaries with two outer keys, dict1 and dict2. The value for each key is another dictionary containing a single key-value pair.

How to Access Values in a Dictionary of Dictionaries

To access values in a dictionary of dictionaries, you can use the following syntax:

dict_of_dicts = {'dict1': {'key1': 'value1'}, 'dict2': {'key2': 'value2'}}
print(dict_of_dicts['dict1']['key1'])

In this example, we are accessing the value of the key1 key in the dict1 dictionary. The output will be value1.

How to Add Values to a Dictionary of Dictionaries

To add values to a dictionary of dictionaries, you can use the following syntax:

dict_of_dicts = {'dict1': {'key1': 'value1'}, 'dict2': {'key2': 'value2'}}
dict_of_dicts['dict1']['key2'] = 'value2'
print(dict_of_dicts)

In this example, we are adding a new key-value pair to the dict1 dictionary. The output will be {‘dict1’: {‘key1’: ‘value1’, ‘key2’: ‘value2’}, ‘dict2’: {‘key2’: ‘value2’}}.

How to Update Values in a Dictionary of Dictionaries

To update values in a dictionary of dictionaries, you can use the following syntax:

dict_of_dicts = {'dict1': {'key1': 'value1'}, 'dict2': {'key2': 'value2'}}
dict_of_dicts['dict1']['key1'] = 'new_value'
print(dict_of_dicts)

In this example, we are updating the value of the key1 key in the dict1 dictionary. The output will be {‘dict1’: {‘key1’: ‘new_value’}, ‘dict2’: {‘key2’: ‘value2’}}.

How to Delete Values from a Dictionary of Dictionaries

To delete values from a dictionary of dictionaries, you can use the following syntax:

dict_of_dicts = {'dict1': {'key1': 'value1'}, 'dict2': {'key2': 'value2'}}
del dict_of_dicts['dict1']['key1']
print(dict_of_dicts)
dict_of_dicts = {'dict1': {'key1': 'value1'}, 'dict2': {'key2': 'value2'}}
del dict_of_dicts['dict1']['key1']
print(dict_of_dicts)

In this example, we are deleting the key1 key from the dict1 dictionary. The output will be {‘dict1’: {}, ‘dict2’: {‘key2’: ‘value2’}}.

How to Loop Through a Dictionary of Dictionaries

To loop through a dictionary of dictionaries, you can use the following syntax:

dict_of_dicts = {'dict1': {'key1': 'value1'}, 'dict2': {'key2': 'value2'}}
for outer_key, inner_dict in dict_of_dicts.items():
    for inner_key, value in inner_dict.items():
        print(f'{outer_key}.{inner_key} = {value}')

In this example, we are looping through each key-value pair in the dictionary of dictionaries and printing the key and value for each pair. The output will be:

dict1.key1 = value1
dict2.key2 = value2

Conclusion

In conclusion, a dictionary of dictionaries is a useful data structure for representing complex data sets in a hierarchical format. It allows developers to easily access and manipulate data at different levels of the hierarchy. By using the examples provided in this article, you should be able to create, access, update, and delete values in a dictionary of dictionaries in Python.