That's now been some days I'm looking for a solution to my problem. I won't cover all the things I tried so far and will just explain my problem.
Here are my two inputs:
unused_field_list = ['a.b', 'b.d.y', 'c.g', 'z']
my_dictionnary =
{
"a": {
"b": {
"key": "value"
},
"c": {
"f": {
"key": "value"
}
}
},
"b": {
"d": {
"y": {
"key": "value"
}
},
"g": {
"key": "value"
}
},
"c": {
"g" : {
"key": "value"
}
},
"z": {
"key": "value"
}
}
Here is the output I would like:
{
"a": {
"c": {
"f": {
"key": "value"
}
}
},
"b": {
"g": {
"key": "value"
}
}
}
So what I try to achieve is removing the "keys" I do have in my unused_field_list
from my dictionary. It's not mandatory but another good thing would be to also totally remove a key if it does not contain lower level keys (like it is the case for b.d
and c
keys in my example. Depth of the dict is not known in advance and varies.
I think this is not doable with a straightforward approach but I really hope there is a way of doing it quite simply.