[Solved] How to compare two list in python? [closed]


If you have the memory to create a set of all tuples from one list then this is the most efficient way. Convert one list to a set of all its tuples and then iterate over the other list. This runs in O(n) time and space complexity.

L1 = [8, 80, 53, 70, 90, 24, 40]
L2 = [30, 255, 70, 90, 24, 0, 36, 54]
 
triples = set()
for i in range(len(L1) - 2):
    triples.add((L1[i], L1[i+1], L1[i+2]))
for i in range(len(L2) - 2):
    triple = (L2[i], L2[i+1], L2[i+2])
    if triple in triples:
        print("Found", triple)

It prints Found (70, 90, 24)

solved How to compare two list in python? [closed]