title: Best approach to convert flat nested data to hierarchical data in python
body: <p>I am representing category hierarchy in flat manner.<br>
  Category Hierarchy is  </p>
  
  <pre><code>category1  
      category4
          category6  
      category5  
          category7
  category2
  category3
  </code></pre>
  
  <p>I am storing this as list using dictionary  </p>
  
  <pre><code>d = [{'id': 1, 'name': 'category1', 'parent_category_id': None, 'level': 1},
       {'id': 2, 'name': 'category2', 'parent_category_id': None, 'level': 1},
       {'id': 3, 'name': 'category3', 'parent_category_id': None, 'level': 1},
       {'id': 4, 'name': 'category4', 'parent_category_id': 1, 'level': 2},
       {'id': 5, 'name': 'category5', 'parent_category_id': 1, 'level': 2},
       {'id': 7, 'name': 'category6', 'parent_category_id': 4, 'level': 3},
       {'id': 7, 'name': 'category7', 'parent_category_id': 5, 'level': 3}]
  </code></pre>
  
  <p>What can be best approach to convert this category list to hierarchical list like</p>
  
  <pre><code>[{'name': 'category1',
    'subcategory': [{'name': 'category4',
                     'subcategory': [{'name': 'category6', 'subcategory': []}]},
                    {'name': 'category5',
                     'subcategory': [{'name': 'category7', 'subcategory': []}]}]},
   {'name': 'category2', 'subcategory': []},
   {'name': 'category3', 'subcategory': []}]
  </code></pre>