title: constructing a tree view from a nested list with duplicate subtrees (using anytree/treelib)
body: <p>I have a nested list like the following:</p>
  
  <pre><code>lst = [['a', 'b', 'e'],      # this e is the branch of b
         ['a', 'f', 'e'],      # this e is the branch of f,
         ['a', 'h', 'i i i']]  # string with spaces
  </code></pre>
  
  <p>and I wanna to construct a tree like:</p>
  
  <pre><code>a
  ├── b
  │   └── e
  ├── f
  |   └── e
  └── h
      └── i i i
  </code></pre>
  
  <p>I want to use either of the two packages: <a href="https://treelib.readthedocs.io/en/latest/treelib.html#treelib.tree.Tree.paths_to_leaves" rel="nofollow noreferrer">treelib</a> and <a href="https://anytree.readthedocs.io/en/latest/" rel="nofollow noreferrer">anytree</a>. I've read many posts and tried many different methods and didn't make it work.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I came up with the following method, but the problems I have now are</p>
  
  <ol>
  <li>the vertical order of the branches (e.g., "b", "f", "h") is not guaranteed (when I have many lists in a list ).</li>
  <li>"e" as a branch of "f" won't show up</li>
  </ol>
  
  <pre><code>from treelib import Node, Tree
  
  # make list flat
  lst = sum([i for i in lst], [])
  
  tree = Tree()
  tree_dict = {}
  
  # create root node
  tree_dict[lst[0]] = tree.create_node(lst[0])
  
  for index, item in enumerate(lst[1:], start=1):
      if item not in tree_dict.keys():
          partent_node = tree_dict[lst[index-1]]
          tree_dict[item] = tree.create_node(item, parent=partent_node)
  
  tree.show()
  </code></pre>