Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Add keys to nested dictionary

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 15 Jan, 2023
Improve Article
Save Article

Addition of keys in dictionaries have been discussed many times, but sometimes, we might have a problem in which we require to alter/add keys in the nested dictionary. This type of problem is common in today’s world with advent of NoSQL databases. Let’s discuss certain ways in which this problem can be solved. 

Method #1 : Using dictionary brackets This task can be easily performed using the naive method of just keep nesting the dictionary brackets with the new value and new key is created on the go and the dictionary is updated. 

Python3




# Python3 code to demonstrate working of
# Update nested dictionary keys
# Using dictionary brackets
 
# initializing dictionary
test_dict = {'GFG' : {'rate' : 4, 'since' : 2012}}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Using dictionary brackets
# Update nested dictionary keys
test_dict['GFG']['rank'] = 1
 
# printing result
print("Dictionary after nested key update : " + str(test_dict))

Output : 

The original dictionary is : {'GFG': {'rate': 4, 'since': 2012}}
Dictionary after nested key update : {'GFG': {'rate': 4, 'since': 2012, 'rank': 1}}

  Method #2 : Using update() This method is used in cases where more than one keys need to be added to the nested dictionaries. The update function accepts the dictionary and added the dictionary with the keys in it. 

Python3




# Python3 code to demonstrate working of
# Update nested dictionary keys
# Using update()
 
# initializing dictionaries
test_dict = {'GFG' : {'rate' : 4, 'since' : 2012}}
upd_dict = {'rank' : 1, 'popularity' : 5}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Using update()
# Update nested dictionary keys
test_dict['GFG'].update(upd_dict)
 
# printing result
print("Dictionary after nested key update : " + str(test_dict))

Output : 

The original dictionary is : {'GFG': {'rate': 4, 'since': 2012}}
Dictionary after nested key update : {'GFG': {'popularity': 5, 'rate': 4, 'since': 2012, 'rank': 1}}

Method #3 : Using setdefault() 

Python3




# Python code to demonstrate add keys to nested dictionary
   
# initializing dictionary
test_dict = {'GFG' : {'rate' : 4, 'since' : 2012}}
   
# printing original dictionary
print ("The original dictionary is : " + str(test_dict))
   
# Add keys to nested dictionary
# Using dict.setdefault()
test_dict.setdefault('GFG', {}).setdefault('rank',1)
   
# printing result
print ("Dictionary after nested key update : " + str(test_dict))
#This code is contributed by Edula Vinay Kumar Reddy

Output

The original dictionary is : {'GFG': {'rate': 4, 'since': 2012}}
Dictionary after nested key update : {'GFG': {'rate': 4, 'since': 2012, 'rank': 1}}

The above code uses dict.setdefault() method to add keys to the nested dictionary. The setdefault() is a pre-defined function in Python and is used to set a value in the dictionary, if the key is not present. If the key is present, it will not change the current value of the key. This method returns the value of the key. In this code, we are setting the key ‘rank’ to the value 1 in the nested dictionary test_dict. This code will run in O(1) time complexity and O(1) space complexity.

Time Complexity: O(1) 
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!