Reputation: 23
So I need to write a python code for getting all the keys in a dictionary with unique values. For example
my_dict = {4:3, 5:3, 6:4, 9:8}
the program should print
[6, 9]
Because the 3 is repeated in the values. I do not know how to see if the values are different and then how to remove them as there is no ".remove()" for dictionaries. I have tried making a list of keys and values, then trying to mutate them in some way, but I have had no luck. Thanks
Upvotes: 0
Views: 299
Reputation: 180391
Count the values with a collections.Counter dict then only keep keys from you dict with a value that is equal to 1 in the Counter dict.
my_dict = {"4":"3", "5":"3", "6":"4", "9":"8"}
from collections import Counter
cn = Counter(my_dict.itervalues())
print([k for k,v in my_dict.iteritems() if cn[v] == 1])
['6', '9']
Upvotes: 4