Seems like you are on the right track, although it may be possible to do something with regular expressions too (unfortunately I do not understand them enough to know if they will help).Something like this should do what you want:d = {'TJMaxx': ['TJ Maxx', 'TJ Max', 'TJX', 'TJMaxx']} SearchTerm = 'TJ Maxx' selected = None # iterate through dictionary, seeing if term in sub list for k in d: if SearchTerm in d: selected = k if selected is not None: ## do something else: ## report invalid entry
It is potentially inefficient in that it has to search through all entries of the dictionary, but even with a hundred thousand retailers it takes less than 0.1 seconds to perform the search on my rubbish computer (with a million it takes about a second).If you were really worried about the search efficiency you could index the dictionary into a list, like so:d = {'TJMaxx': ['TJ Maxx', 'TJ Max', 'TJX', 'TJMaxx']} # make lists of value, key pairs as items in separate lists l = [[],[]] for k in d: for i in d: l[0].append(i) l[1].append(k) SearchTerm = 'TJ Maxx' # search for item in list, return 'key' value if SearchTerm in l[0]: selected = l[1][l[0].index(SearchTerm)] ## do something else: ## report invalid entry
Creating the list takes about five times longer than searching the dictionary, but searching the list takes half the time of searching the dictionary. This means if you can find a way of only creating the list once per time you use the tool (rather than once per search entry), or create the list, print it, then hard code it (instead of the dictionary) you will be able to increase the efficiency. If you change the inputs you will need to recreate the list....