Try the following:
first = [(1, text, text, 1, 2, 3), (1, text, text, 1, 0, 3), ... (1054, text, text, 2, 2, 3)] second = (1, 2, 3, 4, 5 ... 5412) second_set = set(second) data = [x for x in first if x[0] in second_set]
Suppose that at first it has m elements, and the second has n elements.
The sets are hashed, so their search is close to O (1), which gives the overall efficiency of O (m). Finding the second as a list is O (n), providing the overall efficiency of O (m * n).
source share