Python flatten ordereddict
from collections import OrderedDict
def flatten_dict_keep_lists(data):
flat = {}
def _flatten(obj, parent_key=''):
if isinstance(obj, (dict, OrderedDict)):
for k, v in obj.items():
key = k.split(':')[-1] if ':' in k else k
_flatten(v, key)
elif isinstance(obj, list):
# If it's a list of dicts, flatten each item
if all(isinstance(i, (dict, OrderedDict)) for i in obj):
flat[parent_key] = [
flatten_dict_keep_lists(i) for i in obj
]
else:
# List of values, store as is
flat[parent_key] = obj
else:
flat[parent_key] = obj
_flatten(data)
return flat