I have the following two dictionaries:
{1: ['1'], 2: ['1'], 3: ['1'], 4: ['2'], 5: ['1']}
{1: ['2|N|'], 2: ['1|N|']}
I want to take the key of dictionary 2 and link it with the value's of dictionary 1, for a desired output of the following:
{1: ['2|N|'], 2: ['2|N|'], 3: ['2|N|'], 4: ['1|N|'], 5: ['2|N|']}
Basically combining the key, value of dictionary 2 on the value of dictionary 1.
CodePudding user response:
Pretty straightforward solution:
adict = {1: ['1'], 2: ['1'], 3: ['1'], 4: ['2'], 5: ['1']}
mapping = {1: ['2|N|'], 2: ['1|N|']}
for key, value in adict.items():
adict[key] = [mapping[int(k)][0] for k in value]
print(adict)
Output:
{1: ['2|N|'], 2: ['2|N|'], 3: ['2|N|'], 4: ['1|N|'], 5: ['2|N|']}
CodePudding user response:
here is dict comprehension manner of it
{k:b[int(v[0])] for k,v in a.items() }
{1: ['2|N|'], 2: ['2|N|'], 3: ['2|N|'], 4: ['1|N|'], 5: ['2|N|']}