INNER CODE UNIT · Python
flatten
jupyterhub/zero-to-jupyterhub-k8s · tools/compare-values-schema-content.py:51
def flatten(d, parent_key="", sep="."):
"""
Takes a nested dictionary and return all keys flattened using a separator,
so one element returned would for example be "hub.image.tag".
"""
items = []
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
if isinstance(v, MutableMapping):
if v:
items.append(new_key)
items.extend(flatten(v, parent_key=new_key, sep=sep))
else:
items.append(new_key)
else:
items.append(new_key)
if not parent_key:
items = set(items)