Python: finding keys with unique values in a dictionary?
dictionary, python
Solution
I think efficient way if dict is too large would be
countMap = {}
for v in a.itervalues():
countMap[v] = countMap.get(v,0) + 1
uni = [ k for k, v in a.iteritems() if countMap[v] == 1]
Problem
I receive a dictionary as input, and want to return a list of keys for which the dictionary values are unique in the scope of that dictionary. I will clarify with an example. Say my input is dictionary a, constructed as follows: ``` a = dict() a['cat'] = 1 a['fish'] = 1 a['dog'] = 2 # <-- unique a['bat'] = 3 a['aardvark'] = 3 a['snake'] = 4 # <-- unique a['wallaby'] = 5 a['badger'] = 5 ``` The result I expect is `['dog', 'snake']`. There are obvious brute force ways to achieve this, however I wondered if there's a neat Pythonian way to get the job done.