I am dealing with a complex nested dictionary and list data structure. I need to flatten the data and bring all nested items to level 0. See below example for more clarity :
{a:1,b:2,c:{c1:[{c11:1,c12:2,c13:3},{c21:1,c22:2,c23:3}],d1:[{d11:1,d12:2,d13:3},{d21:1,d22:2,d23:3}]},x:1,y:2}
i need to flatten this to:
{a:1,b:2,c_c1_c11:1, c_c1_c12:2,c_c1_c13:3,c_c1_c21:1,c_c1_c22:2,c_c1_c23:3, c_d1,d11:1...and so on}
I took reference from the first answer in this post, but it can only work if i have nested dictionaries, and not if lists are nested within dictionaries and more dictionaries nested within those lists.
I modified the code a bit to fit my use case, but this code doesn't work
def flattenDict(d):
node_map = {}
node_path = []
def nodeRecursiveMap(d, node_path):
for key, val in d.items():
if ((type(val) is not dict)&(type(val) is not list)):
node_map['_'.join(node_path + [key])] = val
if type(val) is list:
def nodeListRecursion(val,node_path):
for element in val:
if ((type(element) is not dict)&(type(element) is not list)) : node_map['_'.join(node_path + [key])] = element
if type(element) is list: nodeListRecursion(element,node_map)
if type(element) is dict: nodeRecursiveMap(element, node_path + [key])
nodeListRecursion(val,node_path)
if type(val) is dict: nodeRecursiveMap(val, node_path + [key])
nodeRecursiveMap(d, node_path)
return node_map
The indentation is getting messed up when i paste my code here. But i would really appreciate any help here.
val = [val]
to process dict values in the same way as list values. – Otey