May-24-2024, 06:37 AM
I think the problem is, you are modifying dict1, but still want to use it to modify dict2 afterwards.
Make new dictionaries, dict3 and dict4 and leave dict1 and dict2 untouched.
I assume you only want to exclude duplicate values, not duplicate keys.
This works for me:
Make new dictionaries, dict3 and dict4 and leave dict1 and dict2 untouched.
I assume you only want to exclude duplicate values, not duplicate keys.
This works for me:
import string from random import choice def makeme(num): value = [choice("ABCDEFGH")] key = alph[num] return (key, value) # keys are A,B,C,D,E dict1 = {makeme(j)[0]:makeme(j)[1] for j in range(5)} # keys are D,E,F,G,H # keys D and E are also in dict1 dict2 = {makeme(j+3)[0]:makeme(j+3)[1] for j in range(5)} # from this you can see dict3 must lose [F] 2 times and lose [E] 1 time # dict1.values = dict_values([['F'], ['E'], ['G'], ['F'], ['G']]) # dict2.values = dict_values([['F'], ['H'], ['E'], ['A'], ['A']]) dict3 = {k: v for k, v in dict1.items() if v not in dict2.values()} # dict3 = {'C': ['G'], 'E': ['G']} # from this you can see dict4 must lose [F] 1 time and lose [E] 1 time # dict1.values = dict_values([['F'], ['E'], ['G'], ['F'], ['G']]) # dict2.values = dict_values([['F'], ['H'], ['E'], ['A'], ['A']]) dict4 = {k: v for k, v in dict2.items() if v not in dict1.values()} # dict4 = {'E': ['H'], 'G': ['A'], 'H': ['A']}