title: Recursively finding paths in a nested Dict
body: <p>I have a nested dictionary:</p>
  
  <pre><code>d = {
   "@timestamp": "2019-01-08T19:33:50.066Z",
   "metricset": {
      "rtt": 2592,
      "name": "filesystem",
      "module": "system"
   },
   "system": {
      "filesystem": {
          "free_files": 9660022,
          "type": "rootfs",
          "device_name": "rootfs",
          "available": 13555355648,
          "files": 9766912,
          "mount_point": "/",
          "total": 19992150016,
          "used": {
              "pct": 0.322,
              "bytes": 6436794368
          },
          "free": 13555355648
      }
   },
   "host": {
      "name": "AA"
   },
   "beat": {
      "name": "AA",
      "hostname": "AA",
      "version": "6.3.2"
    }
  }
  </code></pre>
  
  <p>What I would like to do is write this dictionary to a CSV file. I'd like the headers of the csv to be something like this: </p>
  
  <pre><code>system.filesystem.type
  </code></pre>
  
  <p>where the path is made up by each level separated by a period. I am able to go through the dictionary and get most of the headers I need; however my problem is with duplicate values.</p>
  
  <p>PROBLEM: I recursively go through the dict and grab all the values and put them in a list. Then, I am searching for those values in the dictionary again, but this time saving the path to construct the header. However, with duplicate values (i.e. the value "rootfs"), I am getting only the first key-value ("type": "rootfs") returned. </p>
  
  <p>Here is my traversal to grab all the values from the dict, which does exactly what I want:</p>
  
  <pre><code>def traverse(valuelist, dictionary):
  
      for k,v in dictionary.items():
  
         if isinstance(v, dict):
           traverse(valuelist,v)
         else:
           valuelist.append(v)
  
      return valuelist
  </code></pre>
  
  <p>Now here is the code that grabs the path for each value from the code above:</p>
  
  <pre><code>def getpath(nested_dict, value, prepath=()):
      for k,v in nested_dict.items():
          path = prepath + (k,)
          if v == value: # found value
             return path
          elif hasattr(v, 'items'): # v is a dict
              p = getpath(v, value, path) # recursive call
              if p is not None:
                  return p
  </code></pre>
  
  <p>This part is not my own code. I found it here on SO and would like to modify it to grab every unique path for duplicate values (i.e. For value "rootfs" 1st path: "system.filesystem.type" 2nd path: "system.filesystem.device_name").</p>
  
  <p>Thank you very much, and any help is appreciated!</p>