I have the following dictionary:
d = {
'results.household.land_tenure_access_restitution.has_land_allocated_relation': ['false', 'true'],
"results.household.land_tenure_access_restitution.is_status_of_claim_required": ["false", "true"]
}
I need to create the following:
d2 = {
'results': {
'household': {
'land_tenure_access_restitution': {
'is_status_of_claim_required': ['false', 'true'],
'has_land_allocated_relation': ['false', 'true']
}
}
}
}
I have written the following code:
f = {}
g = {}
for key, value in d.iteritems():
print f
for n, k in enumerate(reversed(key.split('.'))):
if n == 0:
f = {k: d[key]}
else:
f = {k: f}
g.update(f)
However, the dictionary gets overwritten with the latest key value, since the upper-level key is not unique. I get this output:
{
'results': {
'household': {'land_tenure_access_restitution': {
'has_land_allocated_relation': ['false', 'true']
}}}}
How do I achieve the above-mentioned result?