Is there any way to replace the values within a list of nested dictionaries with any 0.0 value if the length of the list of the nested dictionaries is not equal to the max length? Input dictionary is
dict1= {'canada': {'america': [1.8],
'asia': [1.75, 1.4, 2.56],
'europe': [1.86, 1.5, 1.6],
'florida': [1.3,1.2,1],
'africa': [1.27],
'brazil': [1.26]},
'norway': {'australia': [1.23, 1.27],
'africa': [1.24, 1.8, 1.8],
'brazil': [1.25, 1.9, 1.9],
'california': [1.7]}}
Expected Output dictionary is:
output_dict= {'canada': {'america': [1.8,0.0,0.0],
'asia': [1.75, 1.4, 2.56],
'europe': [1.86, 1.5, 1.6],
'florida': [1.3,1.2,1],
'africa': [1.27,0.0,0.0],
'brazil': [1.26,0.0,0.0]},
'norway': {'australia': [1.23, 1.27,0.0],
'africa': [1.24, 1.8, 1.8],
'brazil': [1.25, 1.9, 1.9],
'california': [1.7,0.0,0.0]}}
CodePudding user response:
As long as the structure of the nested dictionaries stays the same, this should do the job:
max_length = 3
for d in dict1.values():
for k in d.keys():
d[k] = [0.0] * (max_length - len(d[k]))