anytree icon indicating copy to clipboard operation
anytree copied to clipboard

Tree from python nested dictionary

Open GiorgioBarnabo opened this issue 3 years ago • 1 comments

Hi there,

Hi there,

is there any easy way to create a tree from a python nested dictionary? For instance, I would like to turn: {'tax': {'a':{'c':{}, 'd':{}}, 'b': {'e':{}, 'f':{}}}}

tax --- a ------- c ------- d --- b ------- e ------- f

In general, I do not understand what format the json_importer takes as input. Could you please provide some examples? It there any online editor to first manually design the tree with nodes and arrows and then export it in the right format?

Thank you.

GiorgioBarnabo avatar Nov 06 '22 17:11 GiorgioBarnabo

This is now supported in bigtree, and can be done this way:

from bigtree import nested_dict_key_to_tree

data = {'tax': {'a':{'c':{}, 'd':{}}, 'b': {'e':{}, 'f':{}}}}
root = nested_dict_key_to_tree(data, child_key=None)
root.show()

This will result in:

tax
├── a
│   ├── c
│   └── d
└── b
    ├── e
    └── f

Answering other questions,

  • Online editor: The documentation has a playground for you to try out codes
  • Design the tree and export it in right format: You can design your own tree and export it with tree_to_nested_dict_key(..., child_key=None)

kayjan avatar Sep 09 '25 17:09 kayjan