Early return (checking conditions in the right order, see given answers) is usually to be preferred performance wise.
If you cannot make use of early return, but instead need arbitrary conditions on the elements, remember that you have (list/dict) comprehensions.
For example
contains_matrix = [
(element in htmlText)
for element in ("element1", "element2", "element3")
]
will yield a list with True
and False
for each of the elements.
The condition you mention in the question can then be formulated as
not contains_matrix[0] and not contains_matrix[1] and contains_matrix[2]
Let me repeat: the same result can be achieved by checking for "element3"
last and returning early.
Dictionaries are even nicer (and more pythonic):
contains_dict = {
element: (element in htmlText)
for element in ("element1", "element2", "element3")
}
Evaluate with:
(
not contains_dict['element1']
and not contains_dict['element2']
and contains_dict['element3']
)
or even
[element for element, contained in contains_dict.items() if contained]
which gives you all elements that are contained in the HTML.