Maybe this helps (C-like, not very pythonish):
#!/usr/bin/python3 OLDLJP = [[['0:0:0'], 1, 2, 3, 4], [['0:0:1'], 1, 2, 5, 4], [['0:0:3'], 1, 2, 9, 3], [['0:0:4'], 1, 2, 8, 1], [['1:0:0'], 1, 4, 3, 6], [['1:0:1'], 1, 5, 7, 8], [['2:1:1'], 1, 4, 3, 4], [['2:3:8'], 1, 1, 1, 1]] NEWLJP = [[['0:0:0'], '1', '2', 3, 4], [['0:0:1'], '1', '2', 5, 4], [['0:0:3'], '1', '2', 9, 3], [['0:0:4'], '1', '2', 8, 1], [['1:0:0'], '1', 4, 3, 6], [['1:0:1'], '1', 5, 7, 8], [['2:1:1'], '1', 4, 3, 4], [['2:3:8'], '1', 1, 1, 1]] def calculategroup(list, start, end): #print(start, end) for x in range(1, 5): equal = True for y in range(start+1, end): if list[y-1][x] != list[y][x]: equal = False break if equal: for y in range(start, end): list[y][x] = str(list[y][x]) def calculate(list): start = 0 for i in range(1, len(list)): if list[i-1][0][0][0] != list[i][0][0][0]: calculategroup(list, start, i) start = i calculategroup(list, start, len(list)) calculate(OLDLJP) print(OLDLJP) if OLDLJP == NEWLJP: print("ok") else: print("wrong") #done