title: Python - create tree from dictionary
body: <p>I have a problem, I must create a function that create a tree from a dictionary of any numbers of elements.
  Example of dictionaries:</p>
  <pre><code>D1 = {'name': 'musica',
        'children': [
            {'name': 'rock',
             'children': [
                 {'name': 'origini', 'children': []},
                 {'name': 'rock&amp;roll', 'children': []},
                 {'name': 'hard rock', 'children': []}]},
            {'name': 'jazz',
             'children': [
                 {'name': 'origini',
                  'children': [{'name': '1900',
                                'children': [{'name': 'origini', 'children': []}]}]},
                 {'name': 'ragtime', 'children': []},
                 {'name': 'swing', 'children': []}]}]}
  
  D2 = {'name': 'html',
        'children': [
            {'name': 'head',
             'children': [
                 {'name': 'meta', 'children': []},
                 {'name': 'title', 'children': []},
                 {'name': 'style', 'children': []}]},
            {'name': 'body',
             'children': [
                 {'name': 'h1', 'children': []},
                 {'name': 'section',
                  'children': [
                      {'name': 'p',
                       'children': [
                           {'name': 'strong', 'children': []},
                           {'name': 'b', 'children': []},
                           {'name': 'em', 'children': []},
                           {'name': 'i', 'children': []}]},
                      {'name': 'p',
                       'children': [
                           {'name': 'q', 'children': []},
                           {'name': 'code', 'children': []},
                           {'name': 'kbd', 'children': []}]},
                      {'name': 'p',
                       'children': [
                           {'name': 'sup', 'children': []},
                           {'name': 'sub', 'children': []}]},
                      {'name': 'p',
                       'children': [
                           {'name': 'span', 'children': []}]}]},
                 {'name': 'footer',
                  'children': [
                      {'name': 'a',
                       'children': [
                           {'name': 'strong', 'children': []}]},
                      {'name': 'a',
                       'children': [
                           {'name': 'strong', 'children': []}]}]}]}]}
  
  D3 = {'name': 'Giovanni di Bicci',
        'children': [
            {'name': 'Cosimo il vecchio',
             'children': [
                 {'name': 'Piero il gottuso',
                  'children': [
                      {'name': 'Lorenzo il magnifico',
                       'children': [
                           {'name': 'Piero II',
                            'children': [
                                {'name': 'Lorenzo II', 'children': []}]},
                           {'name': 'Papa Leone X', 'children': []},
                           {'name': 'Giuliano', 'children': []}]}]},
                 {'name': 'Giovanni Carlo', 'children': []}]},
            {'name': 'Lorenzo',
             'children': [
                 {'name': 'Pierfrancesco',
                  'children': [
                      {'name': 'Lorenzo', 'children': []},
                      {'name': 'Giovanni',
                       'children': [
                           {'name': 'Giovanni dalle Bande Nere',
                            'children': [
                                {'name': 'Lorenzino', 'children': []},
                                {'name': 'Cosimo I',
                                 'children': [
                                     {'name': 'Francesco I',
                                      'children': [
                                          {'name': 'Maria', 'children': []}]},
                                     {'name': 'Ferdinando I',
                                      'children': {}}]}]}]}]}]}]}
  </code></pre>
  <p>Any solution?</p>
  <p>Thanks a lot</p>
  <h1>ADDITION</h1>
  <p>Thank you all for the answers.</p>
  <p>Now I write the full exercise so you can better understand and answer me: Actually I have implemented a class TNode made in these methods:</p>
  <pre><code>class TNode(object):
  
  def __init__(self, name, Sinistra= None, Destra= None):
      self._name = name
      self.Destra = Destra
      self.Sinistra = Sinistra
      self._children = []
      self._copy = []
      self.c = c
      
  def add(self, c): 
      self._children.append(c)
   
  
  def children(self):
      self._copy = self._children
      return self._copy
      
  def height(self):
      h = 1
      for node in self._children:
          h = max(h, node.height() + 1)
      return h
      
  def count(self):
      c = 1
      for node in self._children:
          c += node.count()
      return c
  
   def count_by_name(self, name):
      lst = []
      if self._name == name: 
          lst += [self]
      for node in self._children:
          lst += node.count_by_name(name)
      return lst
      
  def leaves(self):
      leaves_s = []
      if not self._children:
          leaves_s.append(self.c)
      for node in self._children:
          leaves_s.update(node.leaves())
      return len(leaves_s)
      
  def paths(self, name):
      paths_s = set()
      if self._name == name:
          paths_s.add((name,))
      for node in self._children:
          for j in node.paths(name):
              paths_s.add((self._name,)+j)
      return paths_s
  </code></pre>
  <p>I also need to create a function create_tree(d) that, taken a Dictionary &quot;d&quot; that represents a tree, creates the corresponding tree with nodes of type TNode and returns the root. The function must add the children in the same order as they are listed in the lists of the keys 'children'.</p>
  <p>Sorry if initially I did not write all that.</p>
  <p>I fail to create the function, referred to the class, that create a tree from a dictionary.</p>
  <p>I use Python 2.7</p>
  <p>Thanks.</p>