Skip to content
Related Articles

Related Articles

Improve Article

Python – Remove K valued key from Nested Dictionary

  • Last Updated : 14 May, 2020

Sometimes, while working with records, we can have a problem in which we need to perform the removal of a key from nested dictionary whose value us specific to K. This is a common problem and has its application in data domains such as web development. Lets discuss certain ways in which this task can be performed.

Input : test_dict = {‘CS’: {‘priceless’: 6}, ‘is’: {‘better’: 6}, ‘gfg’: {‘best’: 6}}
Output : {‘CS’: {}, ‘gfg’: {}, ‘is’: {}}

Input : test_dict = {‘CS’: {‘priceless’: 9}, ‘is’: {‘better’: 8}, ‘gfg’: {‘best’: 7}}
Output : {‘CS’: {‘priceless’: 9}, ‘is’: {‘better’: 8}, ‘gfg’: {‘best’: 7}}

Method #1 : Using loop + isinstance() + filter()
The combination of above functions can be used to solve this problem. In this, we perform the task of K value using filter() and isinstance() is used to test for nesting dictionary. The dictionary construction is done using loop.




# Python3 code to demonstrate working of 
# Remove K valued key from Nested Dictionary
# Using loop + isinstance() + filter()
  
# initializing dictionary
test_dict = {'gfg' : {'best' : 4, 'good' : 5}, 
             'is' : {'better' : 6, 'educational' : 4},
             'CS' : {'priceless' : 6}}
  
# printing original dictionary
print("The original dictionary : " + str(test_dict))
  
# initializing rem_val
rem_val = 6
  
# Remove K valued key from Nested Dictionary
# Using loop + isinstance() + filter()
def rem_vals(ele):
  global rem_val
  key, val = ele
  return val != rem_val
  
res = dict()
for key, val in test_dict.items():
  if isinstance(val, dict):
    res[key] = dict(filter(rem_vals, val.items()))
  else:
    res[key] = val
  
# printing result 
print("Dictionary after removal : " + str(res)) 
Output :



The original dictionary : {‘is’: {‘educational’: 4, ‘better’: 6}, ‘gfg’: {‘best’: 4, ‘good’: 5}, ‘CS’: {‘priceless’: 6}}
Dictionary after removal : {‘is’: {‘educational’: 4}, ‘gfg’: {‘best’: 4, ‘good’: 5}, ‘CS’: {}}

 

Method #2 : Using dictionary comprehension + isinstance() + lamda
The combination of above functionalities can be used to perform this task in one liner using lambda function.




# Python3 code to demonstrate working of 
# Remove K valued key from Nested Dictionary
# Using dictionary comprehension + isinstance() + lamda
  
# initializing dictionary
test_dict = {'gfg' : {'best' : 4, 'good' : 5}, 
             'is' : {'better' : 6, 'educational' : 4},
             'CS' : {'priceless' : 6}}
  
# printing original dictionary
print("The original dictionary : " + str(test_dict))
  
# initializing rem_val
rem_val = 6
  
# Remove K valued key from Nested Dictionary
# Using dictionary comprehension + isinstance() + lamda
fnc = lambda sub: { key1: fnc(val1) if isinstance(val1, dict) else val1
      for key1, val1 in sub.items() if val1 != rem_val}
res = fnc(test_dict)
  
# printing result 
print("Dictionary after removal : " + str(res)) 
Output :

The original dictionary : {‘is’: {‘educational’: 4, ‘better’: 6}, ‘gfg’: {‘best’: 4, ‘good’: 5}, ‘CS’: {‘priceless’: 6}}
Dictionary after removal : {‘is’: {‘educational’: 4}, ‘gfg’: {‘best’: 4, ‘good’: 5}, ‘CS’: {}}

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :