if last_key == key - 1:
    for val, count in current_sequences:
        if val + 1 in data:
            new_sequences.add((val + 1, count + 1))
data.remove(val + 1)
if best is None or count + 1 > best[0]:
    best = count + 1, val + 1, key
for v in data:
    new_sequences.add((v, 1))
if best is None:
    best = 1, v, key
current_sequences = new_sequences
last_key = key
Most efficient way to find longest incrementing subsequence in a list of lists