0

I have a list of words, and a function that searches for a word by its length and the letters inside the word.

The for loop at the end cycles through the list of possible words (generated by the given length), and it should either print the word if it contains the given letter, or delete the word from the list of possible words if it does not contain the given letter.

How can I achieve this here:

wlist = ['Apple', 'Banana', 'Cherry', 'Donkey']

def wordSearch(wlist):
    posWords = []
    length = int(input('Enter length of word: '))
    print("POSSIBLE WORD(S):")
    for word in wlist:
        stripWord = word.replace(' ', '')
        if len(stripWord) == length:
            print("%s (%s)" % (word, length))
            posWords.append(word)

    while True:
        searchOptions = ['Add Known Letter', 'Exit']
        searchIndex, item = chooseFromMenu(searchOptions)
        if searchIndex == 0:
            letter = input('Enter letter: ')
            print("POSSIBLE WORD(S):")
            for word in posWords:
                if letter in word:
                    print(word)
                else:
                    # remove word from list somehow
        else:
            break

Specifically this block:

letter = input('Enter letter: ')
print("POSSIBLE WORD(S):")
for word in posWords:
    if letter in word:
        print(word)
    else:
        # remove word from list somehow

I've tried to do del word and posWords.pop(word) but neither seem to work.

I also tried .remove() but I cant exactly remember what I did with that.

I'll go into more detail with the problem I'm having here...

If I type in the letter 'e', it should remove 'Banana' from the list of possible words so that when I type 'n' I should only get 'Donkey', not 'Donkey' and 'Banana'.

If that makes sense.

I have just tried posWords.remove(word) and it hasn't done this either...

Miles B
  • 79
  • 6
  • 2
    Does this answer your question? [How to remove items from a list while iterating?](https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating) – Random Davis Jan 04 '21 at 16:19
  • Unfortunately not. I saw that post before I decided to write my own. Solutions didn't solve my problem. – Miles B Jan 04 '21 at 16:21
  • Why don't those solutions work? – Barmar Jan 04 '21 at 16:24
  • Why do you have to remove them while iterating? Do it in two steps: Print all the matching words, then remove all the non-matching words. – Barmar Jan 04 '21 at 16:25
  • The answer to _"How to delete item in list whilst iterating through list?"_ is **no**. Think of a different way to accomplish your goals. – zvone Jan 04 '21 at 16:25
  • @zvone if you're saying it's not possible then fair enough. Just thought there must be a way to do it. (without making separate lists etc) – Miles B Jan 04 '21 at 16:30
  • Yes, there is a way to do it without additional lists, but not while iterating through this list. You could e.g. increment list index in a loop and modify the list while doing that. A similar solution exists in the linked question. – zvone Jan 04 '21 at 16:35

3 Answers3

2

You can iterate over the list in reverse, so that removing an element doesn't affect the rest of the iteration.

for i in range(len(posWords)-1, -1, -1):
    if letter in posWords[i]:
        print(posWords[i])
    else:
        posWords.pop(i)

This is the solution in this answer to the question you said didn't solve your problem.

Note that the argument to pop() is the list index, not a list element.

Barmar
  • 741,623
  • 53
  • 500
  • 612
1

You can use a while loop and only increase the iterator, if no item is removed.

i = 0
while i < len(posWords):
    if letter in word:
        print(word)
        i += 1
    else:
        del posWords[i] # posWords.pop(i) does the same

This works, because if an item is removed, the next items index is reduced by 1 so i now points to the next item

FloLie
  • 1,820
  • 1
  • 7
  • 19
0

If I'm understanding your question properly then the following should work:

letter = input('Enter letter: ')
print("POSSIBLE WORD(S):")
for word in posWords[:]:
    if letter in word:
        print(word)
    else:
        posWords.remove(word)