I have these two dicationaries:
Dict_Employee_ID = {'0': ['0', '1', '2'], '1': ['0', '1', '2']}
Dict_ID_Shift = {'0': ['L', 'D'], '1': ['D', 'E', 'D'], '2': ['None', 'Any', 'Any']}
My goal is to replace the value of the dictionary Dict_Employee_ID
by the value of the dictionary Dict_ID_Shift
when the keys of Dict_ID_Shift
correspond to the value of Dict_Employee_ID
The expected output should look like this:
{
'0': [['L', 'D'], ['D', 'E', 'D'], ['None', 'Any', 'Any']],
'1': [['L', 'D'], ['D', 'E', 'D'], ['None', 'Any', 'Any']]
}
I have tried using the intersection() method but couldn't get the expected results, what would be your strategy ? Thank you
You can use dictionary comprehension and reassign the value to Dict_Employee_ID
as:
Dict_Employee_ID = {'0': ['0', '1', '2'], '1': ['0', '1', '2']}
Dict_ID_Shift = {'0': ['L', 'D'], '1': ['D', 'E', 'D'], '2': ['None', 'Any', 'Any']}
Dict_Employee_ID = {k: [Dict_ID_Shift[e] for e in v] for k, v in Dict_Employee_ID.items()}
where Dict_Employee_ID
will hold the value:
{
'0': [['L', 'D'], ['D', 'E', 'D'], ['None', 'Any', 'Any']],
'1': [['L', 'D'], ['D', 'E', 'D'], ['None', 'Any', 'Any']]
}
If you want to replace in existing dict, then you can also write the explicit for
loop to achieve this as:
for k, v in Dict_Employee_ID.items():
Dict_Employee_ID[k] = [Dict_ID_Shift[e] for e in v]