I need to build a dict in form of a tree from a list. The first keys are the last elements of the list.
current = [['A1'], ['B1', 'A1'], ['B2', 'A1'], ['C1', 'B2', 'A1'], ['D2', 'C1', 'B2', 'A1'], ['D1', 'C1', 'B2', 'A1'], ['A2']]
expected = {
'A1': {
'B1': None,
'B2': {
'C1': {
'D1': None,
'D2': None
},
},
},
'A2': None
}
I have tried something with default dict, but no success in make it recursively, and i don't know if it's the correct way.
result = defaultdict(list)
for e in current:
result[e[-1]].append(e)