I have two Python dictionaries:
A = {1: 27, 3: 41, 7: 26, 11: 32, 12: 23, 14: 23, 15: 37, 18: 27, 21: 35, 23: 12, 25: 33, 26: 7, 27: 1, 29: 14, 32: 11, 33: 25, 35: 21, 36: 27, 37: 15, 38: 25, 39: 23, 41: 3, 44: 27}
and
B = {1: 27, 3: 41, 7: 26, 11: 32, 12: 23, 15: 37, 18: 27, 21: 35, 26: 7, 29: 14, 32: 11, 33: 25, 35: 21, 36: 27, 39: 23, 41: 3, 44: 27}
I want to remove all numerical values that are present in dictionary B from dictionary A. By this, I mean if a number is present in dictionary B as a key or a value, then it must be totally removed from dictionary A from its keys or values (also even if the number is present in one of the values multiple times).
So my desired output should be
38
as rest of all the numbers are present in dictionary B and dictionary A, but 38 is unique one in B (by eyeballing).
So far, I have tried the following:
C = {k: v for k, v in A.items() if k not in B}
This gives me:
C = {14: 23, 23: 12, 25: 33, 27: 1, 37: 15, 38: 25}
Clearly, this is not what I want. I also tried:
C = dict(A.items() - B.items())
Didn't work either.
Any help would be hugely appreciated.
https://stackoverflow.com/questions/65802413/remove-items-both-keys-and-values-from-a-dictionary-if-they-are-present-in-ano January 20, 2021 at 10:51AM
没有评论:
发表评论