Search code examples
pythonfilterany

Program won't display string if it contains one string from list


I want to filter some strings I get. For example I have this string :

str = "I just go on Facebook today"

With a list of banned words like this :

banned_words = ["facebook", "Facebook", "Netflix"]

How could I do something like: "If none of the banned words are in the string", so I can process the string?

With some search I find the function any and try something like :

if any(word not in str for word in banned_words):

But doesn't work at all :/


Solution

  • You should use the following

    if not any(word  for word in banned_words if word in str):print(1)
    

    Note: Never use keywords as variable names. here str is a keyword. so, i suggest you to use some other variable name