[Solved] Python:how to get keys with same values? [closed]


If you want this to work for any arbitrary key(s) you can use a defaultdict of OrderedDicts..

from collections import defaultdict, OrderedDict
result_dict = defaultdict(OrderedDict)
data = [('Han Decane','12333'),('Can Decane','12333'),('AlRight','10110')]
for (v,k) in data:
   result_dict[k][v]=True


>>> list(result_dict['12333'].keys())
['Han Decane', 'Can Decane']

And if you want all the results that had multiple values

>>> [k for k in result_dict if len(result_dict[k]) > 1 ]
['12333']

3

solved Python:how to get keys with same values? [closed]