Reputation: 225
I have two dictionaries and I want to map them based on below conditions :
Below is the example of what Im looking -
resp_dict = {'name': "Rodney",
'prop_9986': "http://rodrnylynch.com",
'prop_7635': "7164084552",
'status': "Active",
'prop_5346': "pkunch"}
map_dict = {'user_name': "name",
'self_website': "prop_9986",
'account_number': "prop_7635",
'page_name': "profile_page"}
map_result = {'user_name': "Rodney",
'self_website': "http://rodrnylynch.com",
'account_number': "7164084552",
'status': "Active",
'prop_5346': "pkunch"}
I tried something like below which basically meets condition 1 & 3 mentioned above but not the 2nd condition.
map_result = dict((k, resp_dict[map_dict[k]]) for k in map_dict if map_dict[k] in resp_dict)
>>> map_result
{'self_website': 'http://rodrnylynch.com', 'user_name': 'Rodney', 'account_number': '7164084552'}
Can someone help me with the best approach to achieve this.
Thank you!
Upvotes: 2
Views: 381
Reputation: 27660
Prepare the reverse name lookup:
name = {v: k for k, v in map_dict.items()}
Then use it:
map_result = {name.get(k, k): v for k, v in resp_dict.items()}
Upvotes: 1