title: What is the best way to implement nested dictionaries?
body: <p>I have a data structure which essentially amounts to a nested dictionary. Let's say it looks like this:</p>
  
  <pre><code>{'new jersey': {'mercer county': {'plumbers': 3,
                                    'programmers': 81},
                  'middlesex county': {'programmers': 81,
                                       'salesmen': 62}},
   'new york': {'queens county': {'plumbers': 9,
                                  'salesmen': 36}}}
  </code></pre>
  
  <p>Now, maintaining and creating this is pretty painful; every time I have a new state/county/profession I have to create the lower layer dictionaries via obnoxious try/catch blocks. Moreover, I have to create annoying nested iterators if I want to go over all the values. </p>
  
  <p>I could also use tuples as keys, like such:</p>
  
  <pre><code>{('new jersey', 'mercer county', 'plumbers'): 3,
   ('new jersey', 'mercer county', 'programmers'): 81,
   ('new jersey', 'middlesex county', 'programmers'): 81,
   ('new jersey', 'middlesex county', 'salesmen'): 62,
   ('new york', 'queens county', 'plumbers'): 9,
   ('new york', 'queens county', 'salesmen'): 36}
  </code></pre>
  
  <p>This makes iterating over the values very simple and natural, but it is more syntactically painful to do things like aggregations and looking at subsets of the dictionary (e.g. if I just want to go state-by-state).</p>
  
  <p>Basically, sometimes I want to think of a nested dictionary as a flat dictionary, and sometimes I want to think of it indeed as a complex hierarchy. I could wrap this all in a class, but it seems like someone might have done this already. Alternatively, it seems like there might be some really elegant syntactical constructions to do this. </p>
  
  <p>How could I do this better?</p>
  
  <p>Addendum: I'm aware of <code>setdefault()</code> but it doesn't really make for clean syntax.  Also, each sub-dictionary you create still needs to have <code>setdefault()</code> manually set.</p>