def merge(master, addition):
    overlap_lens = (i + 1 for i, e in enumerate(addition) if e == master[-1])
for overlap_len in overlap_lens:
    for i in range(overlap_len):
        if master[-overlap_len + i] != addition[i]:
            break
else:
    return master + addition[overlap_len:]
return master + addition
Pythonic way to merge two overlapping lists, preserving order