I have the following code:
mystring = ["reddit", "google"]
mylist = ["a", "b", "c", "d"]
for mystr in mystring:if any(x not in mystr for x in mylist):print(mystr)
I'm expecting that this should return only "google"
. But for some reason it returns both "reddit"
and "google"
.
Your use of any
and not in
contradicts itself. You want to check either for all
like this:
if all(x not in mystr for x in mylist):print mystr
Or just check for not any
(which is more readable in my opinion):
if not any(x in mystr for x in mylist):print mystr
Both of these versions can be a one-liner by the way (instead of your loop), if you use list comprehension (but that is just a matter of taste, or if you prefer to print out one line of results instead of one line for every result):
mystring = ["reddit", "google"]
mylist = ["a", "b", "c", "d"]
print [s for s in mystring if not any(x in s for x in mylist)]