Search code examples
pythondictionarykey

How check if a word is in a list that is a key of a dictionary?


I have a dictionary like this:

dictionary={('I', 'We', 'They'): 'X'}

For example I want to check is there 'I' in this dictionary and if it is true, return 'X'. Is there any solution?


Solution

  • I suppose you want more elements in your dictionary, so you will have to iterate over the keys of your dictionary and return when you found the right key. Like this:

    def find_element(element):
        keys = dictionary.keys()
        for key in keys:
            if element in key:
                return dictionary[key]
    

    You can call this function with "I" as element and you'll find the right value to your key.