I have two sets of dictionaries. Let's call them A and B. I need to be able to create a third dictionary, C, which has all the keys and associated values of A, except for the pairs that are in B. This is what I have tried so far:
A = {"A": {"B": 6, "C": 5}, "B": {"A": 6, "D": 7, "E": 5}, "C": {"A": 5}, "D": {"B": 7, "E": 10, "H": 25},
"E": {"D": 10, "F": 14, "B": 5}, "F": {"E": 14}, "H": {"D": 25}}
B = {"A": "C"}
C = A
for key, value in A.items():
C.update({key: value})
if key in B:
for i in value:
if i not in B.values():
C[key].pop(i)
print(C)
The idea is, given the two dictionaries, to obtain the output:
A = {"A": {"B": 6}, "B": {"A": 6, "D": 7, "E": 5}, "C": {"A": 5}, "D": {"B": 7, "E": 10, "H": 25}, "E": {"D": 10, "F": 14, "B": 5}, "F": {"E": 14}, "H": {"D": 25}}
Instead, the terminal returns an error, saying that the dictionary has changed size during the iteration. What exactly am I doing wrong here?
if i not in B.values():
? I would have expectedif i != B[key]:
, and subsequently optimised away thefor i in value:
loop by only looking forB[key]
invalue