Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
recursive generator solution in Clear category for The Flat Dictionary by juestr
def flatten(dictionary):
def walk(d, context):
for key, value in d.items():
fullkey = f'{context}/{key}' if context else key
if isinstance(value, dict):
if value:
yield from walk(value, fullkey)
else:
yield (fullkey, '')
else:
yield (fullkey, value)
return dict(walk(dictionary, context=''))
Sept. 20, 2022
Comments: