Python – Remove unwanted Keys associations
Last Updated :
10 May, 2020
Sometimes, while working with Python dictionaries, we can have problem in which we need to remove some unwanted keys and its associated nestings. This can have application across many domains including web development and competitive programming. Let’s discuss certain ways in which this task can be performed.
Input : test_dict = {‘best’ : {‘for’ : {‘geeks’ : {‘CS’ : {‘Gfg’ : 12}}}}}
Output : {‘best’: {‘for’: {}}}
Input : test_dict = {‘best’ : 14, ‘gfg’ : 13}
Output : {‘best’ : 14, ‘gfg’ : 13}
Method #1 : Using isinstance()
+ loop + recursion
The combination of above functionalities can be used to solve this problem. In this, we check for the element value being a dictionary or key using isinstance() and we recur for the construction of all the keys that are not present as unwanted keys.
def helper_fnc(test_dict, unw_keys):
temp = {}
for key, val in test_dict.items():
if key in unw_keys:
continue
if isinstance (val, dict ):
temp[key] = helper_fnc(val, unw_keys)
else :
temp[key] = val
return temp
test_dict = { "Gfg" : { 'is' : 45 , 'good' : 15 },
'best' : { 'for' : { 'geeks' : { 'CS' : 12 }}}}
print ( "The original dictionary : " + str (test_dict))
unw_keys = [ 'is' , 'geeks' ]
res = helper_fnc(test_dict, unw_keys)
print ( "The filtered dictionary : " + str (res))
|
Output :
The original dictionary : {‘Gfg’: {‘is’: 45, ‘good’: 15}, ‘best’: {‘for’: {‘geeks’: {‘CS’: 12}}}}
The filtered dictionary : {‘Gfg’: {‘good’: 15}, ‘best’: {‘for’: {}}}
Share your thoughts in the comments
Please Login to comment...