Search code examples
pythonstringdictionarykey-value

How to search if dictionary value contains certain string with Python


I have a dictionary with key-value pair. My value contains strings. How can I search if a specific string exists in the dictionary and return the key that correspond to the key that contains the value.

Let's say I want to search if the string 'Mary' exists in the dictionary value and get the key that contains it. This is what I tried but obviously it doesn't work that way.

#Just an example how the dictionary may look like
myDict = {'age': ['12'], 'address': ['34 Main Street, 212 First Avenue'],
          'firstName': ['Alan', 'Mary-Ann'], 'lastName': ['Stone', 'Lee']}

#Checking if string 'Mary' exists in dictionary value
print 'Mary' in myDict.values()

Is there a better way to do this since I may want to look for a substring of the value stored ('Mary' is a substring of the value 'Mary-Ann').


Solution

  • I am a bit late, but another way is to use list comprehension and the any function, that takes an iterable and returns True whenever one element is True :

    # Checking if string 'Mary' exists in the lists of the dictionary values
    print any(any('Mary' in s for s in subList) for subList in myDict.values())
    

    If you wanna count the number of element that have "Mary" in them, you can use sum():

    # Number of sublists containing 'Mary'
    print sum(any('Mary' in s for s in subList) for subList in myDict.values())
    
    # Number of strings containing 'Mary'
    print sum(sum('Mary' in s for s in subList) for subList in myDict.values())
    

    From these methods, we can easily make functions to check which are the keys or values matching.

    To get the keys containing 'Mary':

    def matchingKeys(dictionary, searchString):
        return [key for key,val in dictionary.items() if any(searchString in s for s in val)]
    

    To get the sublists:

    def matchingValues(dictionary, searchString):
        return [val for val in dictionary.values() if any(searchString in s for s in val)]
    

    To get the strings:

    def matchingValues(dictionary, searchString):
        return [s for s i for val in dictionary.values() if any(searchString in s for s in val)]
    

    To get both:

    def matchingElements(dictionary, searchString):
        return {key:val for key,val in dictionary.items() if any(searchString in s for s in val)}
    

    And if you want to get only the strings containing "Mary", you can do a double list comprehension :

    def matchingStrings(dictionary, searchString):
        return [s for val in dictionary.values() for s in val if searchString in s]