If you open the python interpreter, you will find that "doc" and "pdf" and "xls" and "jpg"it is the same as 'jpg':
>>> "doc" and "pdf" and "xls" and "jpg"
'jpg'
So, instead of testing all the lines, your first attempt only checks the "jpg".
There are several ways to do what you want. The following is not the most obvious, but useful:
if not any(test_string in text for test_string in ["doc", "pdf", "xls", "jpg"]):
filtered.append(text)
Another approach would be to use a loop forin combination with an operator else:
for test_string in ["doc", "pdf", "xls", "jpg"]:
if test_string in text:
break
else:
filtered.append(text)
, :
tofilter = ["one.pdf", "two.txt", "three.jpg", "four.png"]
test_strings = ["doc", "pdf", "xls", "jpg"]
filtered = [s for s in tofilter if not any(t in s for t in test_strings)]
, :
text_list = generate_text_list()
extensions = ['.doc', '.pdf', '.xls', '.jpg']
words = ['some', 'words', 'to', 'filter']
text_list = [text for text in text_list if not text.endswith(tuple(extensions))]
text_list = [text for text in text_list if not any(word in text for word in words)]
; " -", " - " .. , .