title: Generic way to create nested dictionary from flat dictionary in python
body: <p>I'm looking for a generic way to convert a flat dict like this:</p>
  
  <pre><code>{
      "det_0_prod_di_0_field" : "value", 
      "det_0_prod_di_0_adi_0_field" : "value", 
      "det_0_prod_di_0_adi_1_field" : "value", 
      "det_1_prod_di_0_field" : "value", 
      "det_1_prod_di_0_adi_0_field" : "value", 
      "det_1_prod_di_0_adi_1_field" : "value", 
  }
  </code></pre>
  
  <p>Into this:</p>
  
  <pre><code>[
      {
          "det" : {
              "prod" : [{
                  "di" : {
                      "field" : "value", 
                      "adi" : [{"field" : "value"}, {"field" : "value"}]
                  }, 
              }]
          }, 
          "det" : {
              "prod" : [{
                  "di" : {
                      "field" : "value", 
                      "adi" : [{"field" : "value"}, {"field" : "value"}]
                  }, 
              }]
          }
      }
  ]
  </code></pre>
  
  <p>Notice that I have to create lists of dict for items that "repeat"... Also, It needs to be generic, because it can have many "levels of nesting"... every time "_0", "_1" etc appears in a key, a list should be created...</p>
  
  <p>Thanks!</p>