[Solved] How to compare two lists and decide one matches the other? [duplicate]


Here is an efficient solution. For each element in the second list, get the next element of the first one until you have a match. If you reach the end of the first list before you matched all, this is False else True

def check(a, b):
    i = iter(a)
    return all(e in i for e in b)

Or manual version:

def check(a, b):
    i = iter(a)
    for e in b:
        try: 
            while e != next(i):
                pass
        except StopIteration:
            return False
    return True

Test:

for l in [l2, l3, l4, l5, l6]:
    print(check(l1, l))

Output:

False
True
True
False
False

15

solved How to compare two lists and decide one matches the other? [duplicate]