title: recursing a dictionary of lists of dictionaries, etc et al (python)
body: <p>I have a dictionary returned from a yaml configuration file with 4 levels: </p>
  
  <p>item, sections, fields, elements</p>
  
  <pre><code>{
      "tag": "test", 
      "sections": [
          {
              "info": "This is section ONE", 
              "tag": "s1"
          }, 
          {
              "info": "This is section TWO", 
              "fields": [
                  {
                      "info": "This is field ONE", 
                      "tag": "f1"
                  }, 
                  {
                      "info": "This is field TWO", 
                      "tag": "f2", 
                      "elements": [
                          {
                              "info": "This is element", 
                              "tag": "e1", 
                              "type_of": "text_field"
                          }, 
                          {
                              "info": "This is element", 
                              "tag": "e2", 
                              "type_of": "text_field"
                          }, 
                          {
                              "info": "This is element", 
                              "tag": "e3", 
                              "type_of": "text_field"
                          }, 
                          {
                              "info": "This is element", 
                              "tag": "e4", 
                              "type_of": "text_field"
                          }
                      ]
                  }, 
                  {
                      "info": "This is field THREE", 
                      "tag": "f3", 
                      "elements": [
                          {
                              "info": "This is element", 
                              "tag": "e5", 
                              "type_of": "text_field"
                          }, 
                          {
                              "info": "This is element", 
                              "tag": "e6", 
                              "type_of": "text_field"
                          }, 
                          {
                              "info": "This is element", 
                              "tag": "e7", 
                              "type_of": "text_field"
                          }, 
                          {
                              "info": "This is element ONE", 
                              "tag": "e8", 
                              "type_of": "text_field"
                          }
                      ]
                  }
              ], 
              "tag": "s2"
          }, 
          {
              "info": "This is section THREE", 
              "fields": [
                  {
                      "info": "This is field FOUR", 
                      "tag": "f4"
                  }, 
                  {
                      "info": "This is field FIVE", 
                      "tag": "f5"
                  }, 
                  {
                      "info": "This is field SIX", 
                      "tag": "f6"
                  }
              ], 
              "tag": "s3"
          }
      ],
      "type_of": "custom"
  }
  </code></pre>
  
  <h1> </h1>
  
  <pre><code>class T():
  
      def __init__(self):
          self.sections = []
          self.fields = []
          self.elements = []
  
  def rt(y):
      t = T()
  
      def recurse(y):
          for k,v in y.iteritems(): 
              if isinstance(v, list):
                  getattr(t, k).append(v)
                  [recurse(i) for i in v]
              else:
                  setattr(t, k, v)
      recurse(y)
      return t
  </code></pre>
  
  <p>So I need to recurse a dictionary of lists of dictionaries that have lists of dictionaries, et. al., sort them into their types (and then add a reference to the piece it belongs to, but one problem at a time.) and put into an instance of T.</p>
  
  <p>This works, but doesn't trim out anything, i.e. each section is captured, but all the rest (fields, elements) are captured. This probably comp sci 101, but I'm mostly teaching myself so this some sort of sorting algorithm I need to learn about. Any input on improving this appreciated.</p>
  
  <p>EDIT:  This turned out to be more in depth than I expected, and is abstractly more of an opportunity to learn how to walk through arbitrary data structures and pick out what I want or need   </p>