Checking whether a string ends with one of a list of suffixes I want to check if some line ends with any substring defined in some list. Is there a better way than following one?

endsA = ['foo', 'bar', 'fur', 'beer']
endsB = ['door', ... ]

def checkEnds(line, ends):
    for end in ends:
        if line.endswith(end):
            return True

....

if checkEnds(line, endsA):
    do_something()
Yes, there is! Generator comprehensions and the any function to the rescue.

def checkEnds(line, ends):
    return any(line.endswith(end) for end in ends)


Just like the original code, this only checks as many line.endswith(end) as necessary, returning True on the first True result.