This returns the smallest snippet from the content containing all of the given keywords (in any order). This provides the correct solution but I would like to know if it can be made more efficient.import itertools, redef solve(content, keywords):    content = content.strip()    contentWords = content.split()    termPosition = {}    for term in keywords:        index = 0        positions = []        while index is not None:            try:                index = contentWords.index(term)                positions.append(index)                contentWords[index] = None            except:                index = None        termPosition[term] = positions    singlePositions = []    multiplePositions = []    for term in termPosition.keys():        if len(termPosition.get(term)) == 1:            singlePositions.append(termPosition.get(term)[0])        else:            multiplePositions.append(termPosition.get(term))    snippet = content    contentWords = content.split(' ')    for element in itertools.product(*multiplePositions):        tempList = list(element) + singlePositions        minPos = min(tempList)        maxPos = max(tempList) + 1        if len(' '.join(contentWords[minPos:maxPos])) < len(snippet):            snippet = ' '.join(contentWords[minPos:maxPos])    return snippet
Getting the smallest snippet from content containing all keywords
python;performance;algorithm;strings;clustering