I have a dictionary of objects:
dic = {\'k1\':obj1, \'k2\':obj2, \'k3\':obj3, ...}
class MyObject:
def __init__(self,x,y):
self.x=x
self
Ignoring the possibility that samb8s raises, that multiple keys exist for a single value, you can easily reverse the dictionary like this:
reverse_dic = dict((v, k) for k, v in dic.items())
This will return a set of ALL keys that have that value - like a filter by value:
def find_keys(dic, val):
return set(k for k,v in dic.items() if v == val)
regular dict[key] -> value
my_regular_dict = {1: 2, 3: 4, 5: 6}
reverse lookup, dict[value] -> key
my_reverse_lookup = dict([(val, key) for key, val in my_regular_dict.items()])
You can use list comprehension. This code gives you the keys in a list:
>>> a
{1: 2, 3: 4, 9: 4, 5: 6, 7: 8}
>>> [key for key, value in a.items() if value == 4]
[3, 9]