I have an input dictionary - dict_input that has destinations as keys and sources as values. One destination can have one or more sources.
dict_input = {'C411':['C052'],'C052':['C001','C002'], 'C001':['9001'], 'C002':['9002']}
In above dict_input, the terminal destination is C411 whereas the initial sources are 9001 and 9002. I am trying to come up with source paths for the terminal destination C411. Expected output in the form of list -
[['C411', 'C052', 'C001', '9001'], ['C411', 'C052','C002', '9002']]
I have this code:
def get_source(node, dict_input, source=[]):
if node in dict_input:
source.append(node)
for i in dict_input[node]:
if i != node:
get_source(i, dict_input, source)
else:
source.append(node)
else:
source.append(node)
return source
return source
dict_input = {'C052':['C001','C002'], 'C411':['C052'], 'C001':['9001'], 'C002':['9002']}
print(get_source('C411', dict_input, []))
The output is the two source paths clubed into a single list -
['C411', 'C052', 'C001', '9001', 'C002', '9002']
How do I modify my code to get separate list of each source path?