Define a 3 Level Nested Dictionary in Python
Last Updated :
20 Feb, 2024
In Python, dictionaries provide a versatile way to store and organize data. Nested dictionaries, in particular, allow for the creation of multi-level structures. In this article, we’ll explore the process of defining a 3-level nested dictionary and demonstrate various methods to achieve this.
Define a 3-Level Nested Dictionary in Python
Below are some of the ways by which we can define a 3-level nested dictionary in Python:
- Direct Assignment
- Using a loop
- Using defaultdict
- Using dict.setdefault
- Using recursion
Define a 3-Level Nested Dictionary By Direct Assignment
In this approach, we directly assign values to keys in a nested structure. It’s a straightforward approach when the structure is known in advance.
Python3
nested_dict = {
'first_level_key1' : {
'second_level_key1' : {
'third_level_key1' : 'value1' ,
'third_level_key2' : 'value2' ,
},
'second_level_key2' : {
'third_level_key3' : 'value3' ,
'third_level_key4' : 'value4' ,
},
},
'first_level_key2' : {
'second_level_key3' : {
'third_level_key5' : 'value5' ,
'third_level_key6' : 'value6' ,
},
'second_level_key4' : {
'third_level_key7' : 'value7' ,
'third_level_key8' : 'value8' ,
},
},
}
print (nested_dict[ 'first_level_key1' ][ 'second_level_key1' ][ 'third_level_key1' ])
|
Define a 3-Level Nested Dictionary in Python Using a Loop
In this approach, we used a loop to iteratively create nested dictionaries. It’s useful when the key hierarchy is known beforehand.
Python3
nested_dict = {}
keys_hierarchy = [ 'first_level_key1' , 'second_level_key1' , 'third_level_key1' ]
current_dict = nested_dict
for key in keys_hierarchy:
current_dict = current_dict.setdefault(key, {})
current_dict[ 'final_key' ] = 'final_value'
print (nested_dict[ 'first_level_key1' ][ 'second_level_key1' ]
[ 'third_level_key1' ][ 'final_key' ])
|
Define a 3-Level Nested Dictionary Using Defaultdict
In this approach, we used defaultdict
for a more dynamic creation of nested dictionaries. It automatically creates inner dictionaries when a key is accessed for the first time.
Python3
from collections import defaultdict
nested_dict = defaultdict( lambda : defaultdict( dict ))
nested_dict[ 'first_level_key1' ][ 'second_level_key1' ][ 'third_level_key1' ] = 'value1'
nested_dict[ 'first_level_key1' ][ 'second_level_key1' ][ 'third_level_key2' ] = 'value2'
print (nested_dict[ 'first_level_key1' ][ 'second_level_key1' ][ 'third_level_key1' ])
|
Define a 3-Level Nested Dictionary Using dict.setdefault()
In this approach, we used
setdefault(
)
to create nested dictionaries dynamically. It sets the default value for a key if it doesn’t exist.
Python3
nested_dict = {}
nested_dict.setdefault( 'first_level_key1' , {}).setdefault(
'second_level_key1' , {})[ 'third_level_key1' ] = 'value1'
print (nested_dict[ 'first_level_key1' ][ 'second_level_key1' ][ 'third_level_key1' ])
|
Define a 3-Level Nested Dictionary Using Recursion
In this approach, we used a recursive function to add keys to nested dictionaries. It’s a flexible approach for dynamic nested structures.
Python3
def add_nested_key(dictionary, keys, value):
if len (keys) = = 1 :
dictionary[keys[ 0 ]] = value
else :
add_nested_key(dictionary.setdefault(keys[ 0 ], {}), keys[ 1 :], value)
nested_dict = {}
add_nested_key(nested_dict, [ 'first_level_key1' ,
'second_level_key1' , 'third_level_key1' ], 'value1' )
print (nested_dict[ 'first_level_key1' ][ 'second_level_key1' ][ 'third_level_key1' ])
|
Conclusion
In this article we studied about creating a 3-level nested dictionary in Python using various methods. The choice of method depends on the specific requirements of your program, such as the need for dynamic creation, readability, or flexibility. Understanding these methods allows us to structure our data effectively and access values efficiently within the nested structure.
Share your thoughts in the comments
Please Login to comment...