CV88 CV88 - 1 year ago 59
Python Question

Use enumerate to find indices of all words with letter 'x'

How use enumerate to help find the indices of all words containing 'x'

Thank you

wordsFile = open("words.txt", 'r')
words =
wordList = words.split()


for (index, value) in enumerate(wordList):
if value == 'x':

print("These locations contain words containing the letter 'x':\n",indices)

Answer Source

Your code is almost complete:

for (index, value) in enumerate(wordList):
    if 'x' in value:

This checks, for every single word, if there is an x in it. If so, it adds the index to indices.