I have a list called words
containing words which may be in upper or lower case, or some combination of them. Then I have another list called stopwords
which contains only lowercase words. Now I want to go through each word in stopwords
and remove all instances of that word from words
in a case-insensitive manner, but I don't know how to do this. Suggestions?
Example:
words = ['This', 'is', 'a', 'test', 'string']
stopwords = ['this', 'test']
for stopword in stopwords:
if stopword in words:
words.remove(stopword);
print words
The result shown is this: ['This', 'is', 'a', 'string']
The correct return should have been this: ['is', 'a', 'string']
Make your word lowercase so you don't need to worry about casing:
words = ['This', 'is', 'a', 'test', 'string']
stopwords = {'this', 'test'}
print([i for i in words if i.lower() not in stopwords])
Outputs:
['is', 'a', 'string']
As an additional note, per @cricket_007 (and thanks to @chepner for the correction) comment, making stopwords a set would make it more performant. Notice the change to stopwords above making it a set instead of a list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With