@Sjoerd's solution using any()
is definitely the way to go.
I'd also like to point out a recommendation in PEP 8:
- Be consistent in return statements. Either all return statements in a function should return an expression, or none of them should. If any return statement returns an expression, any return statements where no value is returned should explicitly state this as
return None
, and an explicit return statement should be present at the end of the function (if reachable).
In your case, this means that if you were to keep your for
loop, you should also write an explicit return False
instead of returning None
by falling off the end of the function.
I would also suggest a better name for your function: any_suffix_matches(suffixes, string)
. This name leads to more readable code in the caller, and clearly conveys the idea that the function is a true-false test. It also conforms to the PEP 8 naming convention.
if any_suffix_matches(endsA, line):
do_something()
Note that I've also swapped the order of the parameters. Since "contains any of these suffixes" is the test, and the test is applied to some string, the suffixes should be placed closer to the function name. You see this design convention in re.search(pattern, string)
, for example.