I have a list of news titles and would like to check if any title contains any of the keywords in a list.
e.g.
newstitle =['Python is awesome', 'apple is tasty', 'Tom cruise has new movie']
tag = ['Python','Orange', 'android']
If any keywords in tag
are in newstitle
, I want it to return True
.
I know how to do it with a single tag using:
any('Python' in x for x in newstitle)
But how to do it with multiple keywords?
The below code should achieve the required:
any(t in x for x in newstitle for t in tag)
From the docs:
A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it.