from collections import deque
from operator import itemgetter
def convert_tree(lst):
tree = {}
queue = deque([(lst, tree)])
while queue:
entries, branch = queue.popleft()
for key, children in map(itemgetter('key', 'children'), entries):
if children:
queue.append((children, branch.setdefault(key, {})))
else:
branch[key] = ''
return tree
lst = [{'key': 'a', 'children': [{'key': 'a1', 'children': [{'key': 'a11', 'children': []}]}]},
{'key': 'b', 'children': [{'key': 'b1', 'children': [{'key': 'b11', 'children': []}]}]}, ]
print(convert_tree(lst))
ZnJvbSBjb2xsZWN0aW9ucyBpbXBvcnQgZGVxdWUKZnJvbSBvcGVyYXRvciBpbXBvcnQgaXRlbWdldHRlcgoKZGVmIGNvbnZlcnRfdHJlZShsc3QpOgogICAgdHJlZSA9IHt9CiAgICBxdWV1ZSA9IGRlcXVlKFsobHN0LCB0cmVlKV0pCiAgICB3aGlsZSBxdWV1ZToKICAgICAgICBlbnRyaWVzLCBicmFuY2ggPSBxdWV1ZS5wb3BsZWZ0KCkKICAgICAgICBmb3Iga2V5LCBjaGlsZHJlbiBpbiBtYXAoaXRlbWdldHRlcigna2V5JywgJ2NoaWxkcmVuJyksIGVudHJpZXMpOgogICAgICAgICAgICBpZiBjaGlsZHJlbjoKICAgICAgICAgICAgICAgIHF1ZXVlLmFwcGVuZCgoY2hpbGRyZW4sIGJyYW5jaC5zZXRkZWZhdWx0KGtleSwge30pKSkKICAgICAgICAgICAgZWxzZToKICAgICAgICAgICAgICAgIGJyYW5jaFtrZXldID0gJycKICAgIHJldHVybiB0cmVlCgpsc3QgPSBbeydrZXknOiAnYScsICdjaGlsZHJlbic6IFt7J2tleSc6ICdhMScsICdjaGlsZHJlbic6IFt7J2tleSc6ICdhMTEnLCAnY2hpbGRyZW4nOiBbXX1dfV19LAogICAgICAgeydrZXknOiAnYicsICdjaGlsZHJlbic6IFt7J2tleSc6ICdiMScsICdjaGlsZHJlbic6IFt7J2tleSc6ICdiMTEnLCAnY2hpbGRyZW4nOiBbXX1dfV19LCBdCnByaW50KGNvbnZlcnRfdHJlZShsc3QpKQ==