Let's say we have the following...
pract={
"hello": {
"more": 1,
"some": 2,
"yes": [
{
"dicct": 1
},
{
"more_dict": 4
}
]
}
}
I have an embedded dict/list
. I need to get all single values. So in this instance my result would be...
[1, 2, 1, 4]
Ideally, I would like to keep the direct parent, so this would be better...
[("more",1), ("some",2), ("dicct", 1), ("more_dict", 4)]
This is my recursive attempt...
def grab_children(father, result):
if type(father) == type([]):
for e in father:
if isinstance(e, dict):
grab_children(e, result)
else:
for child_key, child_value in father.items():
if isinstance(child_value, dict):
grab_children(child_value, result)
else:
result.append((child_key, child_value))
Which I run as so...
child = []
grab_children(pract, child)
But when I print the child array, I get...
[('more', 1), ('some', 2), ('yes', [{'dicct': 1}, {'more_dict': 4}])]
Clearly not the output I want. What is wrong with my solution?