Python Forum

Full Version: Membership test for an element in a list that is a dict value for a particular key?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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!
"cherry" in a[5]
for key in a.keys():
    if 'cherry' in a[key]:
        print('gotcha!')