0

in the Dictonary given below

Counter({'d': 4, 'b': 3, 'c': 1, 'e': 3, 'a': 2, 'g': 1})

Can I get the keys c,g based on the value 1? Is it possible to get the key based on the data it has.

in other words, is there any reverse relation searching techniques from which we can get the key(s) if the value for the key(s) provided?

Deshwal
  • 3,436
  • 4
  • 35
  • 94

1 Answers1

1

You can just use a list comprehension to filter out the keys based on the value:

>>> from collections import Counter
>>> d = Counter({'d': 4, 'b': 3, 'c': 1, 'e': 3, 'a': 2, 'g': 1})
>>> [k for k, v in d.items() if v == 1] 
['c', 'g']

Note: This also means that the lookups are now linear instead of constant time, since you have to search the whole dictionary to find the keys.

RoadRunner
  • 25,803
  • 6
  • 42
  • 75