Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Flat Dictionary by logicalladybuglifestyle
def flatten(dictionary):
while sum(map(lambda x: type(x) == dict, dictionary.values())):
for idx, inner_d in enumerate(dictionary.copy().items()):
if idx == 0:
dictionary = {}
if type(inner_d[1]) == dict:
if inner_d[1] == {}:
inner_d = [(inner_d[0], '')]
else:
inner_d = [(inner_d[0] + '/' + in_d[0], in_d[1]) for in_d in inner_d[1].items()]
for i in inner_d:
dictionary[i[0]] = i[1]
else:
dictionary[inner_d[0]] = inner_d[1]
return dictionary
June 8, 2020
Comments: