title: Python: Searching a multi-dimensional dictionary for a key
body: <p>I'm using Python's <a href="http://docs.python.org/library/json.html" rel="nofollow">JSON decoding library</a> with Google Maps API. I am trying to obtain the zip code of an address but it sometimes resides in different dictionary key. Here are two examples (I've trimmed the JSON to what is relevant):</p>
  
  <pre><code>placemark1 = {
    "AddressDetails": {
      "Country": {
        "AdministrativeArea": {
          "SubAdministrativeArea": {
            "Locality": {
              "PostalCode": {
                "PostalCodeNumber": "94043"
              }
            }
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>(<a href="http://code.google.com/apis/maps/documentation/javascript/v2/services.html#Geocoding_Structured" rel="nofollow">View full JSON</a>)</p>
  
  <pre><code>placemark2 = {
    "AddressDetails": {
      "Country" : {
        "AdministrativeArea" : {
          "Locality" : {
            "PostalCode" : {
              "PostalCodeNumber" : "11201"
            }
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>(<a href="http://maps.google.com/maps/geo?q=121%20Livingston%20St,%20Brooklyn,%20NY&amp;format=json" rel="nofollow">View full JSON</a>)</p>
  
  <p>So the zipcodes:</p>
  
  <pre><code>zipcode1 = placemark1['AddressDetails']['Country']['AdministrativeArea']['SubAdministrativeArea']['Locality']['PostalCode']['PostalCodeNumber']
  zipcode2 = placemark2['AddressDetails']['Country']['AdministrativeArea']['Locality']['PostalCode']['PostalCodeNumber']
  </code></pre>
  
  <p>Now I was thinking perhaps I should just search the multi-dimensional dictionary for <code>"PostalCodeNumber"</code> key. Does anyone have any idea on how to accomplish this? I want it to look something like this:</p>
  
  <pre><code>&gt;&gt;&gt; just_being_a_dict = {}
  &gt;&gt;&gt; just_a_list = []
  &gt;&gt;&gt; counter_dict = {'Name': 'I like messing things up'}
  &gt;&gt;&gt; get_key('PostalCodeNumber', placemark1)
  "94043"
  &gt;&gt;&gt; get_key('PostalCodeNumber', placemark2)
  "11201"
  &gt;&gt;&gt; for x in (just_being_a_dict, just_a_list, counter_dict):
  ...     get_key('PostalCodeNumber', x) is None
  True
  True
  True
  </code></pre>