user3151828
user3151828

Reputation: 363

Delete strings from a list who do not contain certain words?

For example something like this (although it doesn't work):

list1 = ['hello, how are you?', 'well, who are you', 'what do you want']

desiredwords = ['hello', 'well']

list2 = [x for x in list1 if any(word in list1 for word in desiredwords) in x]

print list2

['hello, how are you?', 'well, who are you'] #Desired output

Anyone know how to do this?

Upvotes: 0

Views: 2238

Answers (1)

Peter DeGlopper
Peter DeGlopper

Reputation: 37319

You're calling any on the wrong generator expression. You want:

list2 = [x for x in list1 if any(word in x for word in desiredwords)]

The difference here is that in your question you're evaluating whether or not any word in your desired words list is a member of list1 (they're not), and then testing whether False (the output of your any call) is in the element of list that you're testing. This of course does not work.

My any version instead checks words in the desired words list against the element under consideration, using the output of any to filter the list.

Note that in on strings does substring matching - this approach will count "oilwell" as a matching "well". If you want that behavior, fine. If not, it gets harder.

Upvotes: 3

Related Questions