sentences = ['This is sentence 1 as an example',
    'This is sentence 1 as another example', 'This is sentence 2',
    'This is sentence 3 as another example ', 'This is sentence 4']
sentences = sentences * 200 * 1000
sentencesProcessed = []

def preprocess():
    global sentences
global sentencesProcessed
sentencesProcessed = [sentence.split(' ') for sentence in sentences]
sentences = None

def GetSentences(word1, word2, position):
    results = []
for sentenceIndex, sentence in enumerate(sentencesProcessed):
    for wordIndex, word in enumerate(sentence[:-position]):
        if word == word1 and sentence[wordIndex + position] == word2:
            results.append(sentenceIndex)
return results

def main():
    preprocess()
results = GetSentences('sentence', 'another', 3)
print('Got', len(results), 'results')
if __name__ == '__main__':
    main()
Most efficient way to index words in a document?