I have two dictionaries. I want to have a third which contains every key/value pair of dict a and dict b, if there is a key:value pair in dict b which has the same key as a key:value pair in dict a then i want to use the key:value pair in dict b. If there is a key:value pair in b which is not in dict a i want the output dict to also contain that key:value pair. I thought “if key in a” would be a way to check if a key from dict b exists in dict a but for some reason it always seems to be true (which i don’t think is the case).
Code
a = {"bill": 22, "ben": 21}
b = {"bez": 38, "ben": 55}
d = {}
for k,v in a.items():
print("\n")
print("when a[k] is ", str(k))
for key,val in b.items():
print("_b key is ",str(key))
if key in a:
print("______ k is in a", str(a[k]))
Desired output
d = {"bill": 22,
"bez": 38,
"ben": 55}
Aucun commentaire:
Enregistrer un commentaire