Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove words from a List in a case-insensitive manner?

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']

like image 869
Ahmad Avatar asked Jan 06 '23 14:01

Ahmad


1 Answers

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.

like image 131
idjaw Avatar answered Jan 12 '23 00:01

idjaw