I have a list like
l = [['A', 'B', 'C', 'D'], ['P', 'Q', 'R', 'S']]
and a dictionary like
d = {'U':'A', 'L':'B', 'M':'C', 'N':'D', 'O':'F'}
I want to map if the values of dictionary are present within the first inner list then it will map the second inner list with dictionary keys, otherwise discard the dictionary keys.
Expected output:
{'U':'P', 'L':'Q', 'M':'R', 'N':'S'}
CodePudding user response:
You can turn l
into its own mapping:
l_map = dict(zip(*l))
Then
{key: l_map.get(value) for key, value in d.items() if value in l_map}
Demo:
In [3]: l_map
Out[3]: {'A': 'P', 'B': 'Q', 'C': 'R', 'D': 'S'}
In [4]: {key: l_map.get(value) for key, value in d.items() if value in l_map}
Out[4]: {'U': 'P', 'L': 'Q', 'M': 'R', 'N': 'S'}