# Find a value between 2 values in a list

**URL:** https://forum.dynamobim.com/t/find-a-value-between-2-values-in-a-list/66028
**Category:** Uncategorized
**Created:** [July 10, 2021, 12:38pm UTC](https://forum.dynamobim.com/t/find-a-value-between-2-values-in-a-list/66028 "2021-07-10T12:38:10Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Deniz\_Maral](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/deniz_maral/32/80323_2.png) [@Deniz\_Maral](https://forum.dynamobim.com/u/Deniz_Maral)
#### Post date: [July 10, 2021, 12:38pm UTC](https://forum.dynamobim.com/t/find-a-value-between-2-values-in-a-list/66028/1 "2021-07-10T12:38:10Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![AmolShah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/amolshah/32/103244_2.png) [@AmolShah](https://forum.dynamobim.com/u/AmolShah)
#### Post date: [July 10, 2021, 3:19pm UTC](https://forum.dynamobim.com/t/find-a-value-between-2-values-in-a-list/66028/2 "2021-07-10T15:19:55Z")

</div>

@Deniz_Maral Try this:  
 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/c/1/c1829f63e9af6d5cf36a7786f45557b28388cbad.png)

```auto
def numBetween(num,numList):
	if min(numList) <= num <= max(numList):
		numList.sort(reverse=True)
		lowerNum = min(numList,key=lambda x:x-num>0)
		upperNum = numList[numList.index(lowerNum)-1]
		return lowerNum,upperNum
	else:
		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])]

```

---

<div class="post-metadata">

### Author: ![Deniz\_Maral](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/deniz_maral/32/80323_2.png) [@Deniz\_Maral](https://forum.dynamobim.com/u/Deniz_Maral)
#### Post date: [July 10, 2021, 3:27pm UTC](https://forum.dynamobim.com/t/find-a-value-between-2-values-in-a-list/66028/3 "2021-07-10T15:27:04Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Vikram\_Subbaiah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/vikram_subbaiah/32/50797_2.png) [@Vikram\_Subbaiah](https://forum.dynamobim.com/u/Vikram_Subbaiah)
#### Post date: [July 10, 2021, 6:15pm UTC](https://forum.dynamobim.com/t/find-a-value-between-2-values-in-a-list/66028/4 "2021-07-10T18:15:57Z")

</div>

Nodes…

 ![within-nodes](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/8/5/85d88f9060cfa776ad3666d4cb1eae30835ab1ba.png)

Design Script …

 ![within](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/3/8/3861284b9e120915208d9bcf4502cb9ffc5552d9.png)

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

[within.dyn](https://forum.dynamobim.com/uploads/short-url/k66BBeh88fApHGmdF0gEehkQIVC.dyn) (27.4 KB)

---

<div class="post-metadata">

### Author: ![Deniz\_Maral](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/deniz_maral/32/80323_2.png) [@Deniz\_Maral](https://forum.dynamobim.com/u/Deniz_Maral)
#### Post date: [July 10, 2021, 9:51pm UTC](https://forum.dynamobim.com/t/find-a-value-between-2-values-in-a-list/66028/5 "2021-07-10T21:51:06Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)
#### Post date: [July 10, 2021, 10:48pm UTC](https://forum.dynamobim.com/t/find-a-value-between-2-values-in-a-list/66028/6 "2021-07-10T22:48:33Z")

</div>

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.
