Assuming length b is 3, the following code will work even if a contains 'k','h','p' so that they don't always follow each other properly, as in:
a=[('test', 'k'), ('jamy', 'k'), ('Park', 'h'), ('kick', 'p'), ('car', 'k'), ('an', 'r'),('rock', 'h'), ('see','k'), ('it','h'),('an', 'o'),('works', 'p')] b = ['k','h','p']
will produce:
[('jamy', 'Park', 'kick'), ('see', 'it', 'works')]
Code:
letters_a = "".join(str(tup[1]) for tup in a) letters_b = "".join(str(letter) for letter in b) regex = re.compile(r'(%s)[^%s]*(%s)[^%s]*(%s)' % (letters_b[0],letters_b[:2],letters_b[1],letters_b,letters_b[2])) #for this example, the above line translates to: #regex = re.compile(r'(k)[^kh]*(h)[^khp]*(p)') match = re.finditer(regex, letters_a) results=[] for m in match: first,second,third = m.start(1), m.start(2), m.start(3) results.append((a[first][0],a[second][0],a[third][0])) print results