Use all()
if not all(target_string in lst for lst in lst_of_lsts):
raise ValueError('One or more lists does not contain "%s"' % (target_string))
The generator gives True
either False
for each individual test, and all()
checks whether they are all true. Since we use a generator, the estimate is lazy, that is, it stops when the first one is False
found without evaluating the full list.
Or if double in
on the same label seems confusing, you can
if not all((target_string in lst) for lst in lst_of_lsts):
raise ValueError('One or more lists does not contain "%s"' % (target_string))
, .