Python Forum
Membership test for an element in a list that is a dict value for a particular key? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Membership test for an element in a list that is a dict value for a particular key? (/thread-37620.html)



Membership test for an element in a list that is a dict value for a particular key? - Mark17 - Jul-01-2022

Suppose:

a = {3:['apple', 'pear'], 5:['grape', 'cherry']}
How can I do a membership test to see if 'cherry' is in the list that is a value for the key 5?

Thanks!


RE: Membership test for an element in a list that is a dict value for a particular key? - deanhystad - Jul-01-2022

"cherry" in a[5]


RE: Membership test for an element in a list that is a dict value for a particular key? - Pedroski55 - Jul-01-2022

for key in a.keys():
    if 'cherry' in a[key]:
        print('gotcha!')