If you want all matches to use only this list comprehension:
>>> from itertools import product >>> matches = [(t,p) for t,p in product(targets,prefixes) if t.startswith(p)] >>> print(matches) [('abar', 'a'), ('cbar', 'c')]
If you just want the first, use the following list comprehension code as a generator expression. This will be a short circuit if you just want to determine if there is any match.
>>> nextmatch = next(((t,p) for t,p in product(targets,prefixes) if t.startswith(p)), None) >>> print(nextmatch) [('abar', 'a')]
source share