I have created a recursive function to fetch the data from a dictionary. The dictionary consists of keys and each key has a list of keys and it goes on. So I need to fetch the flatten list of keys when I give a key input.
My Dict :
data = {"p": ["s1", "s2", "s3", "s4"],
"s1": ["s1s1", "s1s2"],
"s2": [],
"s3": [],
"s4": [],
"s1s1": [],
"s1s2": ["s1s2s1"],
"s1s2s1": []
}
My function :
def get_data(key):
items = data[key]
if items:
for key in items:
items += get_data(key)
return items
when i call get_data("p") it returns
['s1', 's2', 's3', 's4', 's1s1', 's1s2', 's1s2s1', 's1s2s1']
But the expected output is :
['s1', 's2', 's3', 's4', 's1s1', 's1s2', 's1s2s1']
Thanks in advance for any help ...
items?