Giving the following list:
mapping_list = ['location/name', 'location/address/address1', 'location/address/zip', 'location/business/business_name', 'occupant/occupant_type']
How to turn it into a nested dictionary as following where the last value is the last key with an empty string as a default value.
{
"location":
{
"name": "",
"address":
{
"address1": "",
"zip": ""
},
"business":
{
"business_name": ""
}
},
"occupant":
{
"occupant_type": ""
}
}
Note : the given list could be written as such:
mapping_list_of_lists = []
for full_path in mapping_list:
path_list = full_path.split('/')
mapping_list_of_lists.append(path_list)
print(mapping_list_of_lists)
[['location', 'name'], ['location', 'address', 'address1'], ['location', 'address', 'zip'], ['location', 'business', 'business_name'], ['occupant', 'occupant_type']]