Skip to content

Instantly share code, notes, and snippets.

@bpeterso2000
Last active December 29, 2016 21:51
Show Gist options
  • Save bpeterso2000/56cee62b78454c0d7142314fc3619e66 to your computer and use it in GitHub Desktop.
Save bpeterso2000/56cee62b78454c0d7142314fc3619e66 to your computer and use it in GitHub Desktop.
Get pointers to all unique top-level and nested keys
import collections
def get_keys(d, keys=None, pointer=None, sep='.', prefix=''):
if keys is None:
keys = set()
if pointer is None:
pointer = []
for key, value in d.items():
keyptr = pointer + [str(key)]
keys.add(prefix + sep.join(keyptr))
if isinstance(value, collections.Mapping):
keys |= get_keys(value, None, keyptr, sep, prefix)
return keys
keys = (get_keys(i) for i in items)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment