title: Best way to get nested dictionary items
body: <p>The topic is not new and has already been discussed in multiple posts (links at the bottom). However, I felt like the resources are scattered and it is not always clear what the the best approach is. I would also like to introduce some constraints to clearly define the behaviour that I am expecting.</p>
  <p>Say we have a nested dictionary with any number of items and arbitrary depth:</p>
  <pre><code>d = {&quot;a&quot;: {&quot;b&quot;: {&quot;c&quot; : 0}},
       &quot;b&quot;: {&quot;c&quot; : 1},
       &quot;c&quot;: 2}
  </code></pre>
  <p>What is the best way to get its items?</p>
  <p>The naive approach is quite cumbersome, especially when there are many nested levels.</p>
  <pre><code>&gt;&gt;&gt; d[&quot;a&quot;][&quot;b&quot;][&quot;c&quot;]
  0
  </code></pre>
  <p>So the first constraint is that the keys of the items to get must be provided as tuples, for example:</p>
  <pre><code>key = (&quot;a&quot;, &quot;b&quot;, &quot;c&quot;)
  </code></pre>
  <p>The objective now is to create some function that works as follows:</p>
  <pre><code>&gt;&gt;&gt; getitem(d, key)
  0
  </code></pre>
  <p>This format can also conveniently be applied directly as the <code>__getitem__</code> method of a class.</p>
  <p>One more constraint: I want the function to fail noisily when it is asked to get a non-existing key.</p>
  <pre><code>&gt;&gt;&gt; getitem(d, (&quot;asd&quot;,))
  ...
  KeyError: 'asd'
  </code></pre>
  <p>This excludes all solutions that use item getting to vivify the dictionary.</p>
  <p>Finally, please provide low-level code if possible. If you know of a package that solves this problem please explain the underlying mechanism.</p>
  <p><strong>References</strong></p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/635483/what-is-the-best-way-to-implement-nested-dictionaries">What is the best way to implement nested dictionaries?</a></li>
  <li><a href="https://stackoverflow.com/questions/14692690/access-nested-dictionary-items-via-a-list-of-keys/71382335#71382335">Access nested dictionary items via a list of keys?</a></li>
  <li><a href="https://stackoverflow.com/questions/18237343/nested-dictionaries-in-python-with-error-when-accessing-non-existent-key/71408752#71408752">Nested dictionaries in python with error when accessing non-existent key</a></li>
  <li><a href="https://stackoverflow.com/questions/25833613/safe-method-to-get-value-of-nested-dictionary">Safe method to get value of nested dictionary</a></li>
  <li><a href="https://stackoverflow.com/questions/11700798/accessing-values-nested-within-dictionaries/71401783#71401783">Accessing values nested within dictionaries</a></li>
  <li><a href="https://stackoverflow.com/questions/2544055/python-what-are-efficient-techniques-to-deal-with-deeply-nested-data-in-a-flexi/71402472#71402472">python: what are efficient techniques to deal with deeply nested data in a flexible manner?</a></li>
  <li><a href="https://stackoverflow.com/questions/50011078/convenient-way-to-handle-deeply-nested-dictionary-in-python/71395911#71395911">Convenient way to handle deeply nested dictionary in Python</a></li>
  <li><a href="https://stackoverflow.com/questions/42982561/good-way-to-retrieve-list-of-nested-keys/71395423#71395423">Good way to retrieve list of nested keys?</a></li>
  <li><a href="https://stackoverflow.com/questions/57214676/what-is-the-fastest-way-to-return-a-specific-list-within-a-dictionary-within-a-d/71429707#71429707">What is the fastest way to return a specific list within a dictionary within a dictionary?</a></li>
  </ul>