I'm doing something wrong with my code for merging dictionaries.Here's what I got:
output1 = {'children':
{'children':
{'children':
{'name': 'thriller.mp3', 'type': 'file'},
'name': 'pop',
'type': 'folder'},
'name': 'test_pendrive',
'type': 'folder'},
'name': 'media',
'type': 'folder'}
output2 = {'children':
{'children':
{'children':
{'name': 'paranoid.mp3', 'type': 'file'},
'name': 'metal',
'type': 'folder'},
'name': 'test_pendrive',
'type': 'folder'},
'name': 'media',
'type': 'folder'}
My code is looking like this at the moment:
from collections import defaultdict
l1 = list(output2.items())
l2 = list(output.items())
l3 = l1+l2
d1 = defaultdict(list)
for k,v in l3:
d1[k].append(v)
Which gives the output:
{
"name": ["media", "media"],
"type": ["folder", "folder"],
"children": [{
"name": "test_pendrive",
"type": "folder",
"children": {
"name": "dance",
"type": "folder",
"children": {
"name": "billie_jean.mp3",
"type": "file"
}
}
},
{
"name": "test_pendrive",
"type": "folder",
"children": {
"name": "pop",
"type": "folder",
"children": {
"name": "thriller.mp3",
"type": "file"
}
}
}
]
}
That is almost what I want. My final goal is this:
{
"name": ["media", "media"],
"type": ["folder", "folder"],
"children": [{
"name": "test_pendrive",
"type": "folder",
"children": [{
"name": "dance",
"type": "folder",
"children": {
"name": "billie_jean.mp3",
"type": "file"
}
},{
"name": "pop",
"type": "folder",
"children": {
"name": "thriller.mp3",
"type": "file"
}
}]
}
}
Where am I going wrong? Is this the best method to achieve that goal? Thanks in advance.