# Python code error sort sublist

Hi so I created this python code to sort and create a sublist that has the same 1st element and the 2nd element needs to be apart either 24.88ft, 25.15ft, 27.84ft. cannot be 25ft, and 10ft. For some reason, the first sublist broke with one element that is 24.88ft apart. can anyone help me with this I don’t know what could be wrong?

I guess L11, L25 is the cause of your unexpected results.
It is difficult for me to refactor your code without real data.
In general, when you pop an element, the length and index of the original list are already different from what you think.

``````>>> somelist = range(10)
>>> for x in somelist:
...     somelist.remove(x)
>>> somelist
[1, 3, 5, 7, 9]

>>> somelist = range(10)
>>> for x in somelist[:]:
...     somelist.remove(x)
>>> somelist
[]
``````
``````# For demonstration, the original data have been simplified as follows.
# In reality, you can pre-process the data by using the trick on line 29 of your code
list_IN_0 = [
(1,0),(1,3),(1,4),(1,7),(1,8),(1,12),(1,13),(1,16),(1,17),(1,22),(1,23),
(2,0),(2,3),(2,4),(2,7),(2,8),(2,12),(2,13),(2,16),(2,17),(2,22),(2,23),
(3,0),(3,3),(3,4),(3,7),(3,8),(3,12),(3,13),(3,16),(3,17),(3,22),(3,23)
]

# according L21 the logic of if-statement, I suppose: IN(1)=5, IN(2)=10, IN(3)=15, IN(4)=20
# distance = iterate_y - first_y
# we only took (5 < distance < 10) OR (15 < distance < 20)
# So, the result will be => [[(1,7), (1,8), (1,16), (1,17)],
#                            [(2,7), (2,8), (2,16), (2,17)],
#                            [(3,7), (3,8), (3,16), (3,17)]]

# MAIN LOGIC OF THE FUNCTION
# Using "List comprehension" technique
unique_x_values = sorted(set([x_val[0] for x_val in list_IN_0]))
group_list = [
sorted([point for point in list_IN_0 if point[0] == value])
for value in unique_x_values
]
OUT = [
[tracker for tracker in sublist
if tracker[1] - sublist[0][1] == 0
or 5 < tracker[1] - sublist[0][1] < 10
or 15 < tracker[1] - sublist[0][1] < 20
]
for sublist in group_list
]

# Print processing value, you can ignore these lines in Dynamo
print(f'{unique_x_values = }')
print(f'{group_list = }')
print(f'{OUT = }')
``````