-1

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

Thank you

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

indices=[]

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

print("These locations contain words containing the letter 'x':\n",indices)
CV88
  • 87
  • 1
  • 2
  • 10

1 Answers1

1

Your code is almost complete:

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

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

Neil A.
  • 841
  • 7
  • 23