Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First_Half solution in Clear category for The Flat Dictionary by Sillte
def flatten(dictionary):
result_dict = dict()
for key, value in dictionary.items():
if isinstance(value, str) or (not value):
result_dict[key] = value or ""
else:
for suffix, n_value in flatten(value).items():
r_key = key + "/" + suffix
result_dict[r_key] = n_value
return result_dict
if __name__ == '__main__':
test_input = {"key": {"deeper": {"more": {"enough": "value"}}}}
print(' Input: {}'.format(test_input))
print('Output: {}'.format(flatten(test_input)))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert flatten({"key": "value"}) == {"key": "value"}, "Simple"
assert flatten(
{"key": {"deeper": {"more": {"enough": "value"}}}}
) == {"key/deeper/more/enough": "value"}, "Nested"
assert flatten({"empty": {}}) == {"empty": ""}, "Empty value"
assert flatten({"name": {
"first": "One",
"last": "Drone"},
"job": "scout",
"recent": {},
"additional": {
"place": {
"zone": "1",
"cell": "2"}}}
) == {"name/first": "One",
"name/last": "Drone",
"job": "scout",
"recent": "",
"additional/place/zone": "1",
"additional/place/cell": "2"}
print('You all set. Click "Check" now!')
April 5, 2018