Modifying a class list results in no modification of the list
10:37 09 Aug 2026

So, I was trying to create a class called Word, wherein the user would enter commands to narrow down the list of words, via functions outside of Word, in order to obtain an unknown word.

One of the techniques to do this was to eliminate any words in a list that have a certain letter, using the instance method eliminate to do so, and to use a defined function hasno which is called when the user types in hasno followed by an alphabet. Once this operation has successfully been completed, the program should return the remaining number of possibilities. This is all summarised in the simplified script below:

from sys import exit
import re

class Word:
    possibilities = ['APPLE','MANGO','YOYLE','BERRY','DREAM','ISLES','PHONE','MEPAD','POMNI','STEVE','CAINE']
    def __init__(self):
        self.possibilities = Word.possibilities
        self.NumofPossibilities = len(self.possibilities)
    
    def eliminate(self,letter) -> None:
        '''
        Removes a word from the possible list of words if it contains the letter inputted into the method.
        The letter must be one of the 26 letters of the English alphabet only.
        '''
        if re.search(r'\b[A-Z]\b',letter,re.IGNORECASE) and type(letter) == str:
            self.possibilities = [word for word in self.possibilities if not letter.upper() in word]
        else:
            raise TypeError("'letter' argument can only be one of the 26 letters of the alphabet.")
            

def main():
    word = Word()
    while True:
        cmd = input('>>>').strip().upper().split()
        if cmd[0] == 'HASNO' and len(cmd)==2: 
            """command 'hasno' supports only two arguments: the command itself and the letter to be removed"""
            print(hasno(cmd[1],inWord=word))
        elif cmd[0] == 'EXIT':
            exit()
        else:
            pass 
            '''Note: other commands are ignored due to irrelevancy.'''
            
def hasno(*letters: str,inWord: Word):
    for letter in letters:
        inWord.eliminate(letter)
    possibilityNumber = inWord.NumofPossibilities
    return f'\n{possibilityNumber} possibilities.'

if __name__ == '__main__':
    main()

Side notes for the above script:

  • It is assumed that the usage for the command input hasno by the user is correct/valid. (For instance, the user inputs hasno e and not hasno 52 ) This is because the error checks are irrelevant to the point of this post.

  • This script mainly focuses on hasno and not any other irrelevant function like exit

However, when I run this script and input hasno e, rather than the script removing any element from the list and returning 2 possibilities as expected, it instead returns 11 possibilities, the same number of possibilities to begin with. In fact, this is regardless of what letter it is I input to the hasno function. This led me to speculate whether class lists can be modified or not.

I believe that the problem arises from the line:

self.possibilities = [word for word in self.possibilities if not letter.upper() in word]

So, I changed the line to a for loop that removes elements from self.possibilities as follows:

for word in self.possibilities:
    if letter.upper() in word:
        self.possibilities.remove(word)

However, the same problem still arises, even after the change in my script.

Conversely, I believe that this problem can be solved by using a setter for the instance variable self.possibilities, however I'm skeptical of implementing this setter into my program, since the class list Word.possibilities in the real program (reminder that the script above is a simplified version of the actual program.) has thousands of words, and so I see this as a dangerous solution to this matter, because one small line could override a massive list of thousands of elements to a list with just only a few words.

Therefore, I want to know if there is any way I can correctly modify my list without causing any disasters in my code, or if I have gone wrong elsewhere.

python list class