If string equals item from list a, followed by item from list bThis my Answer to the stackoverflow question: If string equals item from list a, followed by item from list b:
I'm using a set for the second list so that you don't have to iterate every time over all it's items.
a = ['hello', 'Hello', 'Hi', 'Greetings', 'Hola'] b = ['world', 'World', 'Planet', 'Earth'] b_set = set(b) needle = 'HelloWorld' for start in a: if needle.startswith(start) and needle[len(start):] in b_set: print 'match'
If you are looking for a shorter version
any((needle[len(start):] in b_set for start in a if needle.startswith(start)))
In contrast to the
itertools.product this solution does not have to compare all
n^2 possible combinations, but has just to walk once over the first list (
n) and in worst case do an additional set lookup.