Search code examples
pythondictionarylist-comparison

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


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()

Solution

  • 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