How to check if elements of a list are in a string
element, if-statement, list, python, string
Solution
The builtin `any()` function can help you here:
black_list = ["ab:", "cd:", "ef:", "gh:"]
for line in some_file:
if ":" in line and not any(x in line for x in black_list):
pass
It's also possible to get the same effect with `all()`:
for line in some_file:
if ":" in line and all(x not in line for x in black_list):
pass
... but I think the first is closer to English, so easier to follow.
Problem
The inverse of this question (finding a string in a list) is so popular, that I wasn't able to find an answer to my question. ``` black_list = ["ab:", "cd:", "ef:", "gh:"] for line in some_file: if ":" in line and black_list not in line: pass ``` This obviously doesn't work. Some iteration over the list needs to happen that returns true/false, but I don't know how to accomplish that elegantly. Thanks.