Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Flat dictionary using Function Recusrsion solution in Clear category for The Flat Dictionary by Stensen
def flatten(d):
flat_dict, new_key = dict(), ""
for key, val in d.items():
new_key, val = key, val if val else ''
if type(val) == str: flat_dict.update({new_key: val})
if type(val) == dict:
for _key, _val in val.items():
new_key += '/' + _key
flat_dict.update(flatten({new_key: _val}))
new_key = new_key[:new_key.rindex("/")]
return flat_dict
# Flat dictionary using Function Recusrsion
# Trim the path each time the key is set so as to add the next one on the same path
Sept. 26, 2020