I am intending to change change some specified values in a nested dictionary using list which have different range of elements. My try is to use loop for that.
here is the dictionary I want to change:
created_dataset [0] = { "Region 1" : { "commnet ": " Approved "},
{ " exchanges" : { 'amount': 1.0,
'type': 'production',
'product': 'low-alloyed',
'name': 'market for steel',
'unit': 'kilogram',
'location': 'Region 1',
'database': 'ecoinvent'},
{ 'amount': 0.378,
'type': 'technosphere',
'product': 'low-alloyed',
'name': 'EAF',
'unit': 'kilogram',
'location': 'Region 1',
'database': 'ecoinvent'},
{ 'amount': 0.09,
'type': 'technosphere',
'product': 'low-alloyed',
'name': 'BOF',
'unit': 'kilogram',
'location': 'Region 1',
'database': 'Steel'},
{ "Region 2" : { "commnet ": " Not Approved "},
{ " exchanges" : { 'amount': 1.0,
'type': 'production',
'product': 'low-alloyed',
'name': 'market for steel',
'unit': 'kilogram',
'location': 'Region 1',
'database': 'ecoinvent'},
{ 'amount': 0.378,
'type': 'technosphere',
'product': 'low-alloyed',
'name': 'EAF',
'unit': 'kilogram',
'location': 'Region 1',
'database': 'ecoinvent'},
{ 'amount': 0.09,
'type': 'technosphere',
'product': 'low-alloyed',
'name': 'BOF',
'unit': 'kilogram',
'location': 'Region 1',
'database': 'Steel'} }
}
I am intend to loop through some inner keys and change their values to from lists below:.
Regions = [ 'Region 1' , 'Region 2']
Productions = ['Technology 1' , 'Technology 2','Technology 3','Technology 4']
Amounts_Region1 = [x1, x2, x3, x4]
Amounts_Region2 = [y1, y2, y3, y4]
to change the values for name of production technologies as for ânameâ to those in Productions list if 'type'== 'technosphere'
and also change the value âamountâ to those in Amounts_Region list respectively for Region 1 and Region 2.
(amount in Region 1 for Technology 1 = x1 , in Region 2 for Technology 1 = y1 , and so on like this)
My try is as below:
for r in Regions:
for i in range (len(created_dataset[0]["r"]['exchanges']):
if created_dataset[0]["r"]['exchanges'][i]['type'] == 'technosphere':
for a in range (len (Productions):
created_dataset[0]["r"]['exchanges'][i]['name'] = Productions[a]
for x in range (len( Amounts_Region1): # and same loop as for Region 2
created_dataset[0]["Region 1"]['exchanges'][i]['amount'] = Amounts_Region1[x]
Unfortunately, this wonât lead to what I expect cause the range of lists indexes are not equal and only when they have same number of elements, will run.
I would appreciate any help and suggestion to solve this issue