I have made a binary search tree with the help of the following library. I have made a comparison function to determine where the nodes should go. The comparison function determines the current y value for each segment.
Look at the following example:

from sortedcontainers import SortedList
def findCurrentY(segment):
#uses current_x to calculate value of y...
def compare(segment):
position = findCurrentY(segment)
return position
global current_x
myList = SortedList(key = compare)
segments = [segment1,segment2]
current_x = 1
for segment in segments:
myList.add(segment)
print(MyList)
current_x = 2
print(MyList)
current_x = 3
print(MyList)
This is how my output looks like
For current_x = 1:
MyList = [segment2,segment1] #y value of segment1 is higher than segment2
For current_x = 2:
MyList = [segment2,segment1]
For current_x = 3:
MyList = [segment2,segment1]
It shows three times the same thing, as it only calculated the compare function ones. How can I dynamically change the compare function when my current_x changes without deleting each element and adding it again to my list?
So it needs to look like this.
For current_x = 1:
MyList = [segment2,segment1] #segment 1 has higher y value
For current_x = 2:
MyList = [segment2,segment1] #segment 1 has higher y value
For current_x = 3:
MyList = [segment1,segment2] #segment 1 has **lower** y value