d1={101:{‘Sender’:1,’Receiver’:3,’Start date’:’14-03-2020′,’Delivery date’:’25-03-2020′,’Sender location’:’Area 1′,’Receiver location’:’Area 6′,’Delivery status’:’Delivered’,’Shipping cost’:198}, 102:{‘Sender’:4,’Receiver’:1,’Start date’:’18-06-2020′,’Delivery date’:’09-07-2020′,’Sender location’:’Area 2′,’Receiver location’:’Area 4′,’Delivery status’:’Delivered’,’Shipping cost’:275}, 103:{‘Sender’:2,’Receiver’:3,’Start date’:’01-12-2020′,’Delivery date’:’Null’,’Sender location’:’Area 5′,’Receiver location’:’Area 1′,’Delivery status’:’In Transit’,’Shipping cost’:200}, 104:{‘Sender’:1,’Receiver’:5,’Start date’:’23-06-2020′,’Delivery date’:’25-06-2020′,’Sender location’:’Area 1′,’Receiver location’:’Area 4′,’Delivery status’:’Delivered’,’Shipping cost’:314}, 105:{‘Sender’:3,’Receiver’:4,’Start date’:’29-08-2020′,’Delivery date’:’10-09-2020′,’Sender location’:’Area 5′,’Receiver location’:’Area 3′,’Delivery status’:’Delivered’,’Shipping cost’:275}, 106:{‘Sender’:5,’Receiver’:2,’Start date’:’28-06-2020′,’Delivery date’:’Null’,’Sender location’:’Area 3′,’Receiver location’:’Area 1′,’Delivery status’:’In Transit’,’Shipping cost’:270}}
d2 = {1:’Phillip’,2:’Omega lll’,3 :’Ramya’,4:’Romesh’,5:’John’}
Advertisement
Answer
You can loop through the first dictionary and replace the id with their name from the second dictionary, if the id is in the second dictionary:
d1={101:{'Sender':1,'Receiver':3,'Start date':'14-03-2020','Delivery date':'25-03-2020','Sender location':'Area 1','Receiver location':'Area 6','Delivery status':'Delivered','Shipping cost':198}, 102:{'Sender':4,'Receiver':1,'Start date':'18-06-2020','Delivery date':'09-07-2020','Sender location':'Area 2','Receiver location':'Area 4','Delivery status':'Delivered','Shipping cost':275}, 103:{'Sender':2,'Receiver':3,'Start date':'01-12-2020','Delivery date':'Null','Sender location':'Area 5','Receiver location':'Area 1','Delivery status':'In Transit','Shipping cost':200}, 104:{'Sender':1,'Receiver':5,'Start date':'23-06-2020','Delivery date':'25-06-2020','Sender location':'Area 1','Receiver location':'Area 4','Delivery status':'Delivered','Shipping cost':314}, 105:{'Sender':3,'Receiver':4,'Start date':'29-08-2020','Delivery date':'10-09-2020','Sender location':'Area 5','Receiver location':'Area 3','Delivery status':'Delivered','Shipping cost':275}, 106:{'Sender':5,'Receiver':2,'Start date':'28-06-2020','Delivery date':'Null','Sender location':'Area 3','Receiver location':'Area 1','Delivery status':'In Transit','Shipping cost':270}} d2 = {1:'Phillip',2:'Omega lll',3 :'Ramya',4:'Romesh',5:'John'} result = {} for key, value in d1.items(): new_value = {k: d2[v] if k in ('Sender', 'Receiver') else v for k,v in value.items()} result[key] = new_value print(result)
Or, you can also use dictionary comprehension to calculate all this:
result = {key:{k: d2[v] if k in ('Sender', 'Receiver') else v for k,v in value.items()} for key, value in d1.items()}