How to match a string value with dictionary value stored as list?

user3787457 picture user3787457 · Jul 12, 2014 · Viewed 9.2k times · Source

Suppose i have a dictionary

A1={'b1':['X','0'],'b2':'Empty',.............,}

and then i have a string

item=X

now i want to match the above string value only with the dict value because other is getting updated dynamically and only string is the way to search.I tried the below code but in vain

for key,value in A1.iteritems():
        if value==item:
            print A1.keys()

Answer

Reuben picture Reuben · Jul 12, 2014

I assume if the value matches then you want the key ?

A1 = {'b1': ['X', '0'], 'b2': ['S', 'T'], 'b3': ['X', 'Y']}

item = 'X'

for key, value in A1.iteritems():
    if item in value:
        print key