How to search if dictionary value contains certain string with Python
Solution 1
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]
Solution 2
You can do it like this:
#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']}
def search(values, searchFor):
for k in values:
for v in values[k]:
if searchFor in v:
return k
return None
#Checking if string 'Mary' exists in dictionary value
print search(myDict, 'Mary') #prints firstName
Solution 3
Klaus solution has less overhead, on the other hand this one may be more readable
myDict = {'age': ['12'], 'address': ['34 Main Street, 212 First Avenue'],
'firstName': ['Alan', 'Mary-Ann'], 'lastName': ['Stone', 'Lee']}
def search(myDict, lookup):
for key, value in myDict.items():
for v in value:
if lookup in v:
return key
search(myDict, 'Mary')
Solution 4
import re
for i in range(len(myDict.values())):
for j in range(len(myDict.values()[i])):
match=re.search(r'Mary', myDict.values()[i][j])
if match:
print match.group() #Mary
print myDict.keys()[i] #firstName
print myDict.values()[i][j] #Mary-Ann
Solution 5
>>> myDict
{'lastName': ['Stone', 'Lee'], 'age': ['12'], 'firstName': ['Alan', 'Mary-Ann'],
'address': ['34 Main Street, 212 First Avenue']}
>>> Set = set()
>>> not ['' for Key, Values in myDict.items() for Value in Values if 'Mary' in Value and Set.add(Key)] and list(Set)
['firstName']
Related videos on Youtube
Cryssie
Updated on October 18, 2021Comments
-
Cryssie over 2 years
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').
-
Chetan Sharma almost 9 yearswhat will be performance issue in this, if suppose I have 1 million records and I'm implementing this for Auto complete search.
-
wisbucky over 8 years@Unknown You should definitely not do this for your case. You need to index the values in a separate reverse lookup dictionary.
-
Erik over 7 yearsAlternatively, if you know the dictionary is huge, you could convert it to JSON and perform a regex search such as
pattern = r'\w+(?=": [\S\s][^:]+{})'.format(search_text)
to get the key. It won't be faster for smaller dictionaries, but it will be for larger ones, in particular when the data is in the worst case position -
Shushiro over 6 yearsI just learned, that search.a is not a smart way to make a globally available. currently working it out, this may help: python-textbok.readthedocs.io/en/1.0/Variables_and_Scope.html
-
Shushiro over 6 yearssolution found via:stackoverflow.com/questions/7129285/…
-
Shushiro over 6 yearsdef search(myDict, search1): a=[] for key, value in myDict.items(): if search1 in value: a.append(key) return a x=search(myDict, 'anyName') print(str(x))
-
Shushiro over 6 yearsdef search(myDict, search1): a=[] for key, value in myDict.items(): if search1 in value: a.append(key) return a x=search(myDict, 'anyName') print(str(x))
-
MazeOfEncryption almost 6 yearsAh, gotcha. You could do it in one line like this:
search_dict = lambda x, y: ((k if y in v else None for v in x[k]) for k in x)
-
Ivo Merchiers over 5 yearsI think the formatting in your answer is a bit off. Take a look at stackoverflow.com/editing-help
-
Andrea Ciufo about 3 yearsI get an error
list indices must be integers or slices, not list
-
Andrea Ciufo about 3 yearsFor getting the keys, based on the dictionary used by the user in the question it returns this error message
<ipython-input-7-b5d6370c2444> in <genexpr>(.0) ----> 1 print (sum(1 for key,val in myDict if 'Mary' in val) > 0) ValueError: too many values to unpack (expected 2)
and is the same for thematchingKeys
function<ipython-input-2-65db50c6e286> in <listcomp>(.0) 1 def matchingKeys(dictionary, searchString): ----> 2 return [key for key,val in dictionary if searchString in val] ValueError: too many values to unpack (expected 2)
-
Naeio about 3 years@AndreaCiufo Oops my bad, I forgot to use the method
dict.items
. I wanted to give the concept, the idea of using one line list comprehension but by reading it all over again, I realize I have made many mistakes -
Andrea Ciufo about 3 yearsyep this happens because there is a list of strings associated with keys and not a single string :)
-
shantanu pathak about 3 years@AndreaCiufo give ur code .. i think you dict contains multiple values in a single key..
-
Andrea Ciufo about 3 yearsI used the question code, and actually contains a list of strings and was similar to @Nei oversight here :)
-
Naeio about 3 yearsExactly, and I forgot the
dict.items
:) IDK what I was thinking about when writing my answer, I guess I went too fast -
shantanu pathak about 3 years@AndreaCiufo is the problem fixed now ?
-
paranormaldist about 3 yearsHow might someone search multiple strings? using this approach
-
Naeio about 3 years@paranormaldist Well, you have to take a list or tuple of strings as input and then compare each string of the dict to each string in the input. So that would mean replacing
any(searchString in s for s in val)
byany(any(searched in s) for s in val for searched in searchString)
or something like that. But it gets quite long on only one line, so maybe you should better do it on multiple lines or divide it into functions