title: How to find all occurrence of a key in nested dict, but also keep track of the outer dict key value?
body: <p>I've searched over stackoverflow and found the following code that allow me to search for a key values in nested dict recursively. However, I also want to keep track of the outer dict's key value. How should I do that?</p>
  
  <p>from Alfe's answer in the below link, I can use the code below get all the values of the key in nested dict.
  <a href="https://stackoverflow.com/questions/9807634/find-all-occurrences-of-a-key-in-nested-python-dictionaries-and-lists">Find all occurrences of a key in nested python dictionaries and lists</a></p>
  
  <pre><code>data = {'item1': {
    'name': 'dummy',
    'type': 'dummy1'},
  
  'item2': {
    'name': 'dummy',
    'type': 'dummy1',
    'label':'label2'
  },
  
  'item3': {
    'name': 'dummy',
    'type': 'dummy1',
    'label':'label3'},
  
  'item4': {
    'name': 'dummy',
    'type': 'dummy1'}
  }
  
   def find(key, dictionary):
      for k, v in dictionary.items():
          if k == key:
              yield v
          elif isinstance(v, dict):
              for result in find(key, v):
                  yield result
          elif isinstance(v, list):
              for d in v:
                  for result in find(key, d):
                      yield result
  
  
  In[1]:list(find('label', data))
  Out[1]: 
  ['label2', 'label3']
  </code></pre>
  
  <p>However, I also need to keep record of the outer dict key as below. How should I do this? Also my data can potentially have more than one layer.</p>
  
  <pre><code>{'item2':'label2',
  'item3':'label3'
  }
  </code></pre>
  
  <p>I also find the recursive_lookup in this link very neatly written. However, it's returning <code>None</code> when I tried to run it.</p>
  
  <p><a href="https://stackoverflow.com/questions/48314755/find-keys-in-nested-dictionary">Find keys in nested dictionary</a></p>
  
  <pre><code>def recursive_lookup(k, d):
      if k in d:
          return d[k]
      for v in d.values():
          if isinstance(v, dict):
              return recursive_lookup(k, v)
      return None
  </code></pre>
  
  <p>It's returning <code>None</code> when I call <code>recursive_lookup('label', data)</code>.</p>
  
  <p>If anyone can point out for me why the above code is not working that would be great too!</p>