title: Creating a nested dictionary from a flattened dictionary
body: <p>I have a flattened dictionary which I want to make into a nested one, of the form</p>
  
  <pre><code>flat = {'X_a_one': 10,
          'X_a_two': 20, 
          'X_b_one': 10,
          'X_b_two': 20, 
          'Y_a_one': 10,
          'Y_a_two': 20,
          'Y_b_one': 10,
          'Y_b_two': 20}
  </code></pre>
  
  <p>I want to convert it to the form</p>
  
  <pre><code>nested = {'X': {'a': {'one': 10,
                        'two': 20}, 
                  'b': {'one': 10,
                        'two': 20}}, 
            'Y': {'a': {'one': 10,
                        'two': 20},
                  'b': {'one': 10,
                        'two': 20}}}
  </code></pre>
  
  <p>The structure of the flat dictionary is such that there should not be any problems with ambiguities. I want it to work for dictionaries of arbitrary depth, but performance is not really an issue. I've seen lots of methods for flattening a nested dictionary, but basically none for nesting a flattened dictionary. The values stored in the dictionary are either scalars or strings, never iterables.</p>
  
  <p>So far I have got something which can take the input </p>
  
  <pre><code>test_dict = {'X_a_one': '10',
               'X_b_one': '10',
               'X_c_one': '10'}
  </code></pre>
  
  <p>to the output</p>
  
  <pre><code>test_out = {'X': {'a_one': '10', 
                    'b_one': '10', 
                    'c_one': '10'}}
  </code></pre>
  
  <p>using the code</p>
  
  <pre><code>def nest_once(inp_dict):
      out = {}
      if isinstance(inp_dict, dict):
          for key, val in inp_dict.items():
              if '_' in key:
                  head, tail = key.split('_', 1)
  
                  if head not in out.keys():
                      out[head] = {tail: val}
                  else:
                      out[head].update({tail: val})
              else:
                  out[key] = val
      return out
  
  test_out = nest_once(test_dict)
  </code></pre>
  
  <p>But I'm having trouble working out how to make this into something which recursively creates all levels of the dictionary.</p>
  
  <p>Any help would be appreciated!</p>
  
  <p>(As for why I want to do this: I have a file whose structure is equivalent to a nested dict, and I want to store this file's contents in the attributes dictionary of a NetCDF file and retrieve it later. However NetCDF only allows you to put flat dictionaries as the attributes, so I want to unflatten the dictionary I previously stored in the NetCDF file.)</p>