Phil
Phil

Reputation: 14651

Filtering out items from a list using nested list comprehensions in Python

I have two lists. One contains sentences, the other contains words.

I want to have all the sentences, which do NOT contain any of the words from the list of words.

I'm trying to achieve this with list comprehensions. Example:

cleared_sentences = [sentence for sentence in sentences if banned_word for word in words not in sentence]

However, it doesn't seem to be working as I get an error telling me that a variable is used before assignment.

I've tried looking for nested comprehensions and I am sure this must have been asked for but I can not find anything.

How can I achieve this?

Upvotes: 1

Views: 555

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121814

You got the order mixed up:

[sentence for sentence in sentences for word in words if banned_word not in sentence]

Not that that'll work as that'll list the sentence every time a banned word does show up in the sentence. Take a look at the fully expanded nested loops version:

for sentence in sentences:
    for word in words:
        if banned_word not in sentence:
            result.append(sentence)

Use the any() function to test for banned words instead:

[sentence for sentence in sentences if not any(banned_word in sentence for banned_word in words)]

any() loops over the generator expression only until a True value is found; it'll stop doing work the moment a banned word is found in the sentence. This is more efficient at least.

Upvotes: 3

Related Questions