Find a value between 2 values in a list

I am trying to find the the levels up and down for an element by its elevation. I would like to find, between which levels that element is.
For example:

Elevations = [3.5,4,10]
Levels = [0,3,6,9,12]

Expected Output = [[3,6], [3,6], [9,12]]

thank you very much!

@Deniz_Maral Try this:

def numBetween(num,numList):
	if min(numList) <= num <= max(numList):
		lowerNum = min(numList,key=lambda x:x-num>0)
		upperNum = numList[numList.index(lowerNum)-1]
		return lowerNum,upperNum
		return "Number out of range"
toList = lambda x : x if hasattr(x, '__iter__') else [x]

OUT = [numBetween(x, IN[1]) for x in toList(IN[0])]

Thanks! Great work!
It should work like that! I will inform you on monday :slight_smile:

1 Like


Design Script …

List.GetItemAtIndex(b,List.Transpose([List.IndexOf((a<1>-b > 0)<1>,false)-1,List.IndexOf((b-a<1> > 0)<1>,true)]));

within.dyn (27.4 KB)


Would it be too complicated if list of levels is not sorted? I have sorted the list so as to make calculation easier. After finding the elevation of levels I would do like that: Level.ByElevation > atIndex [0] and then assign the offset to element…

I have to create a level between these two levels and if there are more elements with different elevations, I have to take the smallest elevation of element between these levels and place with offset to new level.

I think that sorting the list, then asking for the first elevation that is more than the target be best. The first true would be the level above, and the prior index would be the left below.

1 Like