This question is related to Flatten nested dictionaries, compressing keys. I used that idea and modified to unnest list of dictionary but unable to do.
My input will be a dictionary which can have nested dictonaries or nested lists (list of dictionaries) as shown in the input to the function, flatten_dict_list
Below is my source code:
def flatten_dict_list(d, parent_key='', sep='.'):
items = []
for k, v in d.items():
print("***")
new_key = parent_key + sep + k if parent_key else k
print(new_key)
if isinstance(v, list):
for z in v:
if isinstance(z, collections.abc.MutableMapping):
print(type(z))
print(z)
items.extend(flatten_dict_list(z, new_key, sep=sep).items())
else:
print(type(z))
print(z)
items.append((new_key, z))
else:
if isinstance(v, collections.abc.MutableMapping):
print(type(v))
print(v)
items.extend(flatten_dict_list(v, new_key, sep=sep).items())
else:
print(type(v))
print(v)
items.append((new_key, v))
return dict(items)
print(flatten_dict_list({'a': 1, 'c': [{'a': 2, 'b': {'x': 5, 'y' : 10}}, {'test': 9999}, [{'s': 2}, {'t': 100}]], 'd': [1, 2, 3]}))
Expected output is: {'a': 1, 'c.a': 2, 'c.b.x': 5, 'c.b.y': 10, 'c.test': 9999, 'c.s': 2, 'c.t': 100, 'd': 3}
My actual result is: {'a': 1, 'c.a': 2, 'c.b.x': 5, 'c.b.y': 10, 'c.test': 9999, 'c': [{'s': 2}, {'t': 100}], 'd': 3}