Open In App

Define a Nested Dictionary in Python

Last Updated : 13 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Python, dictionaries are powerful data structures that allow you to store and organize data using key-value pairs. When you need to represent complex and hierarchical data, nested dictionaries come in handy. A nested dictionary is a dictionary that contains one or more dictionaries as values. In this article, we will explore some simple methods to define a nested dictionary in Python.

How to Define a Nested Dictionary in Python?

Below, are the methods for How to Define a Nested Dictionary in Python.

Define a Nested Dictionary Using Curly Braces

The most straightforward way to create a nested dictionary is by using curly braces {}. You can nest dictionaries by placing one set of curly braces inside another. In this example, the outer dictionary has a key ‘outer_key’ with a corresponding value that is another dictionary with keys ‘inner_key1’ and ‘inner_key2’.

Python3




# Using Curly Braces
nested_dict = {'outer_key': {'inner_key1': 'value1',
                              'inner_key2': 'value2'}}
print(nested_dict)


Output

{'outer_key': {'inner_key1': 'value1', 'inner_key2': 'value2'}}

Define a Nested Dictionary in Python Using a For Loop

If you have a large number of nested keys or want to dynamically create a nested dictionary, you can use loops to build the structure. This method allows you to iterate through lists of outer and inner keys, dynamically creating a nested dictionary structure.

Python3




outer_keys = ['outer_key1', 'outer_key2']
inner_keys = ['inner_key1', 'inner_key2']
 
nested_dict = {outer_key: {inner_key: f'value_{outer_key}_{inner_key}'
                           for inner_key in inner_keys}
               for outer_key in outer_keys}
 
print(nested_dict)


Output

{'outer_key1': {'inner_key1': 'value_outer_key1_inner_key1', 'inner_key2': 'value_outer_key1_inner_key2'}, 
'outer_key2': {'inner_key1': 'value_outer_key2_inner_key1', 'inner_key2': 'value_outer_key2_i......

Define a Nested Dictionary Using dict() Constructor

Another method to create a nested dictionary is by using the dict() constructor and specifying key-value pairs for the inner dictionaries. This method provides a more explicit way to define nested dictionaries by calling the dict() constructor for the outer dictionary and providing key-value pairs for the inner dictionaries.

Python3




nested_dict = dict(outer_key=dict(inner_key1='value1',
                                   inner_key2='value2'))
print(nested_dict)


Output

{'outer_key': {'inner_key1': 'value1', 'inner_key2': 'value2'}}

Define a Nested Dictionary Using defaultdict

The defaultdict class from the collections module can be employed to create a nested dictionary with default values for missing keys. This is particularly useful when you plan to update the inner dictionaries later. In this example, the defaultdict is initialized with the default value type of dict. It automatically creates inner dictionaries for each outer key, allowing you to append values to them.

Python3




from collections import defaultdict
 
nested_dict = defaultdict(dict)
nested_dict['outer_key']['inner_key1'] = 'value1'
nested_dict['outer_key']['inner_key2'] = 'value2'
print(dict(nested_dict))


Output

{'outer_key': {'inner_key1': 'value1', 'inner_key2': 'value2'}}



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads