while i != len(L):
    for j in range(len(L[i])):
        if abs(L[i][j] - L[i - 1][j]) > 1:
            D.append(L.pop(i))
i -= 1
break
for d in D:
    ins = True
for j in range(len(L[-1])):
    if abs(L[-1][j] - d[j]) > 1:
        ins = False
break
if ins:
    L.append(d)
D.remove(d)
i += 1
return L
for i in f(3, 5):
    pass
How to order permutations so that at least 1 element in each permutation differs by exactly 1