1

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried the list comprehension (the latter) and I still get an error. Is there any chance it might need to be .... any(banned_word in sentence for banned_word in words)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.