Skip to content

Commit

Permalink
Unflatten for lists by benbowen
Browse files Browse the repository at this point in the history
unflatten does not support lists, the github user benbowen created an
issue: ianlini#8 and described a
solution that worked for him.
  • Loading branch information
AlexTelon committed Oct 25, 2019
1 parent 1d75bf8 commit 37fa60b
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/flatten_dict/flatten_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,25 @@ def nested_set_dict(d, keys, value):
assert keys
key = keys[0]
if len(keys) == 1:
if key in d:
raise ValueError("duplicated key '{}'".format(key))
d[key] = value
if type(d) == list:
d.append(value)
else:
d[key] = value
return
d = d.setdefault(key, {})

# the type is a string so make a dict if none exists
if type(keys[1]) == int:
if key in d:
pass
else:
d[key] = []
d = d[key]
elif type(key)==int:
if (key+1) > len(d):
d.append({})
d = d[key]
else:
d = d.setdefault(key, {})
nested_set_dict(d, keys[1:], value)


Expand Down

0 comments on commit 37fa60b

Please sign in to comment.