How to Initialize a Dictionary of Dictionaries in Python?

This tutorial will discuss about a unique way to initialize a dictionary of dictionaries in Python.

Suppose we have two lists one is a list of keys and second is the inner keys. Like this,

listOfKeys = ['Atharv', 'Avisha', 'Kartik', 'Justin']
listOfInnerKeys = ['Maths', 'Science', 'Social', 'English']

Now we want to initialise a dictionary of dictionaries from these two lists. The items in the first list listOfKeys should act as a key in the dictionary and for each key we will have another nested dictionary as the value. This inner dictionary should have the keys from the list listOfInnerKeys and their corresponding value should be the default value 93. Like this,

{'Atharv': {'Maths': 93,
            'Science': 93,
            'Social': 93,
            'English': 93},
 'Avisha': {'Maths': 93,
            'Science': 93,
            'Social': 93,
            'English': 93},
 'Kartik': {'Maths': 93,
            'Science': 93,
            'Social': 93,
            'English': 93},
 'Justin': {'Maths': 93,
            'Science': 93,
            'Social': 93,
            'English': 93}}

All this can be done using Dictionary Comprehension, Where we will first use the dictionary comprehension to create a nested dictionary, using the list listOfKeys. Each key will have default value 93 associated with it then we will use this next nested dictionary as a default value and initialise a another dictionary using dictionary comprehension where keys will be picked from the list listOfKeys and each key will have default value i.e. copy of nested dictionary, which we created in the previous step. Like this,

# Created a nested dictionary from listOfInnerKeys with default value
nestedDict = {innerKey: defaultValue for innerKey in listOfInnerKeys}

# Created a Dictionary from listOfKeys with nestedDict as default value
dictObj = {key: nestedDict.copy() for key in listOfKeys}

So this is how we can initialise a Dictionary of dictionaries in Python.

Let’s see the complete example,

listOfKeys = ['Atharv', 'Avisha', 'Kartik', 'Justin']
listOfInnerKeys = ['Maths', 'Science', 'Social', 'English']

defaultValue = 93

# Created a nested dictionary from listOfInnerKeys with default value
nestedDict = {innerKey: defaultValue for innerKey in listOfInnerKeys}

# Created a Dictionary from listOfKeys with nestedDict as default value
dictObj = {key: nestedDict.copy() for key in listOfKeys}

print(dictObj)

Output

{'Atharv': {'Maths': 93, 'Science': 93, 'Social': 93, 'English': 93}, 'Avisha': {'Maths': 93, 'Science': 93, 'Social': 93, 'English': 93}, 'Kartik': {'Maths': 93, 'Science': 93, 'Social': 93, 'English': 93}, 'Justin': {'Maths': 93, 'Science': 93, 'Social': 93, 'English': 93}}

Summary

We learned how to Initialize a Dictionary of Dictionaries in Python.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top