Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
11-liner: easiest by stack solution in Clear category for The Flat Dictionary by przemyslaw.daniel
def flatten(dictionary):
stack, result = [((), dictionary)], {}
while stack:
path, current = stack.pop()
for k, v in current.items():
if isinstance(v, dict) and v:
stack += [(path+(k,), v)]
else:
key = '/'.join(path+(k,))
result[key] = ["", v][bool(v)]
return result
March 21, 2018
Comments: