I have a dictionary that has keys associated with lists.
mydict = {\'fruits\': [\'banana\', \'apple\', \'orange\'],
\'vegetables\': [\'pepper\', \'carr
mydict = {'fruits': ['banana', 'apple', 'orange'],
'vegetables': ['pepper', 'carrot'],
'cheese': ['swiss', 'cheddar', 'brie']}
item = "cheddar"
for key, values in mydict.iteritems():
if item in values:
print key
If you are going to do a lot of this kind of searching, I think you can create a reverse index for the original mydict
to speed up querying:
reverse_index = {}
for k, values in mydict.iteritems():
for v in values:
reverse_index[v] = k
print reverse_index.get("cheddar")
print reverse_index.get("banana")
This way you don't have to traverse the values
list every time to find an item.