Python – Remove K value items from dictionary nesting
Last Updated :
27 Mar, 2023
Given dictionary with multiple nestings, remove all the keys with value K.
Input : [{“Gfg” : {“a” : 5, “b” : 8, “c” : 9}}, {“is” : {“j” : 8, “k” : 10}}, {“Best” : {“i” : 16}}], K = 8
Output : [{‘a’: 5}, {‘c’: 9}, {‘k’: 10}, {‘i’: 16}]
Explanation : All the keys with value 8, (“b”, “j”) has been removed.
Input : [{“Gfg” : {“a” : 5, “b” : 8, “c” : 9}}, {“is” : {“j” : 8, “k” : 10}}, {“Best” : {“i” : 16}}], K = 5
Output : [{‘c’: 9}, {‘k’: 10}, {‘i’: 16}, {“j” : 8}, {“b” : 8}]
Explanation : “a” with 5 as value removed.
Method #1 : Using loop + dictionary comprehension
The combination of above functions can be used to solve this problem. In this, we iterate for all the nesting keys using loop. and remove values. This approach is for single-level nesting of dictionary.
Python3
test_list = [{ "Gfg" : { "a" : 5 , "b" : 8 , "c" : 9 }},
{ "is" : { "f" : 8 , "j" : 8 , "k" : 10 }},
{ "Best" : { "i" : 16 }}]
print ( "The original list is : " + str (test_list))
K = 8
res = list ()
for sub in test_list:
for key, val in sub.items():
for key1, val1 in val.items():
if val1 ! = K:
res.append({key1: val1})
print ( "The dictionary after value removal : " + str (res))
|
Output
The original list is : [{‘Gfg’: {‘a’: 5, ‘b’: 8, ‘c’: 9}}, {‘is’: {‘f’: 8, ‘j’: 8, ‘k’: 10}}, {‘Best’: {‘i’: 16}}] The dictionary after value removal : [{‘a’: 5}, {‘c’: 9}, {‘k’: 10}, {‘i’: 16}]
Method #2 : Using recursion + loop (For multiple nesting)
This is yet another way in which this task can be performed. In this we solve a more generic problem of catering to inner dictionary elements as well using recursion.
Python3
res = []
def hlper_fnc(test_dict):
for key in test_dict:
if type (test_dict[key]) = = dict :
hlper_fnc(test_dict[key])
else :
if test_dict[key] ! = K:
res.append({key: test_dict[key]})
test_dict = { "Gfg" : { "a" : 5 , "b" : 8 , "c" : 9 },
"is" : { "f" : 8 , "l" : { "j" : 8 , "k" : 10 }},
"Best" : { "i" : { "k" : { "o" : 8 }}}}
print ( "The original dictionary is : " + str (test_dict))
K = 8
hlper_fnc(test_dict)
print ( "The dictionary after value removal : " + str (res))
|
Output
The original dictionary is : {‘Gfg’: {‘a’: 5, ‘b’: 8, ‘c’: 9}, ‘is’: {‘f’: 8, ‘l’: {‘j’: 8, ‘k’: 10}}, ‘Best’: {‘i’: {‘k’: {‘o’: 8}}}} The dictionary after value removal : [{‘a’: 5}, {‘c’: 9}, {‘k’: 10}]
Method #3: Using list comprehension
Step-by-step algorithm:
- Define a list test_list of dictionaries.
- Define a variable K that will hold the value to be removed from the nested dictionaries.
- Define a list comprehension that iterates over each dictionary in test_list, each key-value pair in that dictionary, and each key-value pair in the nested dictionary, and filters out any nested dictionary value that is equal to K. Append each filtered key-value pair to the list res.
- Print the resulting list of dictionaries res.
Python3
test_list = [{ "Gfg" : { "a" : 5 , "b" : 8 , "c" : 9 }}, {
"is" : { "f" : 8 , "j" : 8 , "k" : 10 }}, { "Best" : { "i" : 16 }}]
K = 8
print ( "The original dictionary is : " + str (test_list))
res = [{key1: val1} for sub in test_list for key, val in sub.items()
for key1, val1 in val.items() if val1 ! = K]
print ( "The dictionary after value removal: " + str (res))
|
Output
The original dictionary is : [{'Gfg': {'a': 5, 'b': 8, 'c': 9}}, {'is': {'f': 8, 'j': 8, 'k': 10}}, {'Best': {'i': 16}}]
The dictionary after value removal: [{'a': 5}, {'c': 9}, {'k': 10}, {'i': 16}]
Time Complexity: O(nm), where n is the number of dictionaries in test_list and m is the total number of key-value pairs in all the nested dictionaries.
Auxiliary Space: O(nm), where n is the number of dictionaries in test_list and m is the total number of key-value pairs in all the nested dictionaries.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...