title: Check key existence in nested dictionaries
body: <p>I am working with API calls and thus Python dictionaries.</p>
  
  <p>However, for the same request, I don't always key the same keys, and I'd like to know when I can call a key without an Exception...</p>
  
  <p>Let's say I have</p>
  
  <pre><code>test = {'a':{'b':{'c':{'d':'e'}}}}
  </code></pre>
  
  <p>Sometimes key d will exist, sometimes it won't. Sometimes c won't even exist.</p>
  
  <p>I'd like to, somehow, check if <code>test['a']['b']['c']['d']</code> exists, in one line.</p>
  
  <p>What I've tried so far:</p>
  
  <ul>
  <li><p>Using <code>test.get('a', {}).get('b', {}).get('c', {}).get('d', {})</code>. Works fine but it's a mess, sometimes I have 5-6 nested dictionaries with really long names...</p></li>
  <li><p>Using try/except block which is nice, but usually if <code>test['a']['b']['c']['d']</code> does not exist, I will try calling <code>test['a']['b']['e']['f']</code> to check if that one exists, thus I would need to add a try/catch for every of my if statements, as if I am not wrong, if an exception is catch, try block is not executed anymore.</p></li>
  </ul>
  
  <p>I was maybe trying to look to a somekind of reflexive way to do it, calling a function with the name of my "object" as a string, that would check if every key exists, and if it does, return the object itself.</p>
  
  <p>Any thoughts?</p>
  
  <p>The usage behind it would be, omitting useless case, and assuming sometimes the info is in test['a']['b']['c']['d'], sometimes in test['a']['b']['f'] :</p>
  
  <pre><code>if test['a']['b']['c']['d'] **exists**:
      do sthg with the value of test['a']['b']['c']['d']
  elif test['a']['b']['f'] **exists**:
      do sthg else with the value of test['a']['b']['f']
  else:
      do sthg different
  </code></pre>
  
  <p>If I put a try/except there, won't the first exception stop the execution and don't let me execute the elif?</p>
  
  <p>Moreover, I really like the way of calling <code>test['a']['b']['c']['d']</code> better than giving a list of keys. In fact, I want it to be as transparent as possible for me and for the people who will read/use my code.</p>