Make filter
a tuple and use str.startswith()
, it takes either one string or a tuple of strings to test for:
filter = tuple(filter)
[s for s in to_check if s.startswith(filter)]
Demo:
>>> filter = ("/This/is/an/example", "/Another/example")
>>> to_check = ["/This/is/an/example/of/what/I/mean", "/Another/example/this/is/", "/This/example", "/Another/freaking/example"]
>>> [s for s in to_check if s.startswith(filter)]
['/This/is/an/example/of/what/I/mean', '/Another/example/this/is/']
Be careful, when prefix-matching with paths, you generally want to append trailing path separators so that /foo/bar
doesn't match /foo/bar_and_more/
paths.