I used imran an excellent answer to hide nested Python dictionaries, compress keys, and I'm trying to come up with a way to further smooth out dictionaries that can be inside the values of a list of dictionary elements.
(Of course, since my data usually comes from XML, it can also be recursive ...)
from pprint import pprint
from collections import MutableMapping
def flatten(d, parent_key='', sep='_'):
items = []
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
if isinstance(v, MutableMapping):
items.extend(flatten(v, new_key, sep=sep).items())
else:
items.append((new_key, v))
return dict(items)
Given a dict das follows:
d = {"a": 1,
"b": 2,
"c": {"sub-a": "one",
"sub-b": "two",
"sub-c": "thre"}}
this works fine:
pprint(flatten(d))
{'a': 1,
'b': 2,
'c_sub-a': 'one',
'c_sub-b': 'two',
'c_sub-c': 'thre'}
However, I would like to repeat the value of the list of dict elements once again and check whether each dict in the list can be further smoothed.
Here is an example input example with c-listas the value of a nested list:
d = {"a": 1,
"b": 2,
"c-list": [
{"id": 1, "nested": {"sub-a": "one", "sub-b": "two", "sub-c": "thre"} },
{"id": 2, "nested": {"sub-a": "one", "sub-b": "two", "sub-c": "thre"} },
{"id": 3, "nested": {"sub-a": "one", "sub-b": "two", "sub-c": "thre"} }]}
Here is what I am getting with the function above:
pprint(flatten(d))
{'a': 1,
'b': 2,
'c-list': [{'id': 1, 'nested': {'sub-a': 'one', 'sub-b': 'two', 'sub-c': 'thre'}},
{'id': 2, 'nested': {'sub-a': 'one', 'sub-b': 'two', 'sub-c': 'thre'}},
{'id': 3, 'nested': {'sub-a': 'one', 'sub-b': 'two', 'sub-c': 'thre'}}]}
, , flatten():
{'a': 1,
'b': 2,
'c-list': [{'id': 1, 'nested_sub-a': 'one', 'nested_sub-b': 'two', 'nested_sub-c': 'thre'},
{'id': 2, 'nested_sub-a': 'one', 'nested_sub-b': 'two', 'nested_sub-c': 'thre'},
{'id': 3, 'nested_sub-a': 'one', 'nested_sub-b': 'two', 'nested_sub-c': 'thre'}]}
, "" dict , ... .