Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking two python dictionaries

I'm struggling on the code to link two python dictionaries together:

d1 = {'a':'Apple','b':'Banana'}
d2 = {'Apple':'Pomme','Banana':'Banane'}

I want to create a new dictionary d3 where 'a' is a key for the value 'Pomme' and 'b' is the key for value 'Banane'.

d3 = {'a':'Pomme','b':'Banane'}

Any guidance would be greatly appreciated.

like image 756
bruh Avatar asked Dec 24 '22 06:12

bruh


1 Answers

Using a dictionary comprehension:

d3 = {k: d2[v] for k, v in d1.items()}
like image 58
jpp Avatar answered Mar 08 '23 01:03

jpp