How to create a list of list from the given input? [closed]

You could make a list of lists, which would be better than dynamicaly making new list names.

Something like this:

input_list = [[278, 207], [695, 205], [672, 205], [325, 205], [302, 205],[650, 167], [556, 167], [533, 167]]

output_list = [[input_list[0]]] # Create an output list, with the first element of input_list as a list of lists. 
x = 0
for i in range(1, len(input_list)):
    if abs(input_list[i-1][1]- input_list[i][1]) > 2: # Check if absolute difference > 2
        x += 1
        output_list.append([]) # Create new sublist
    output_list[x].append(input_list[i]) # append sublist to output_list

print(output_list)

Leave a Comment