# Filter elements by different condition input by user

**URL:** <https://forum.dynamobim.com/t/filter-elements-by-different-condition-input-by-user/58080>\
**Category:** Revit\
**Tags:** python, dynamo\
**Created:** [December 7, 2020, 10:33pm UTC](https://forum.dynamobim.com/t/filter-elements-by-different-condition-input-by-user/58080 "2020-12-07T22:33:27Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mons.reseland](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@mons.reseland](https://forum.dynamobim.com/u/mons.reseland)\
**Post date:** [December 7, 2020, 10:33pm UTC](https://forum.dynamobim.com/t/filter-elements-by-different-condition-input-by-user/58080/1 "2020-12-07T22:33:28Z")

</div>

Hi,  
Im trying to making a script that places electrical components in our model, based in where the ventilation engineer has placed their components, “a shadow object”. This i have figured out how to do for one object at a time, but i want it to be a bit more dynamic. I have made an Excel schedule where the rows represent a different filter for the linked elements from the vent.model. Here i want the user of the script to have the option to specify the name, type, and parameters of the familys in the vent.model they want to filter by. I also want the option til select what contrition to filter by: N/V=dont filter by this contition E = Equals, C=Contains, BW= Beginswith and EW=Endswith.

 ![IBujK6qGFt](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/f/9/f9b49d0c9f94e48225e7bdea43f7781c2b03c466.png)  
Here is the problem i have now. How can I filter the elements by different conditions? I have tried, very poorly, to make a python script that does what i have tried to describe here, but it did not work, i’m no expert in python, but i want to become better at it, so if someone can solve it with python that would teach me a lot 🙂 but nodes works also.  
 ![c57BOjX9ob](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/3/3/337ab0c46f7543f198b89dfa6264f565e488cd08.png)

---

<div class="post-metadata">

**Author:** ![Mepdot](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/mepdot/32/106281_2.png) [@Mepdot](https://forum.dynamobim.com/u/Mepdot)\
**Post date:** [December 8, 2020, 5:02am UTC](https://forum.dynamobim.com/t/filter-elements-by-different-condition-input-by-user/58080/2 "2020-12-08T05:02:22Z")

</div>

@mons.reseland

Can you post those files?  
Revit, Excel, and .dyn

---

<div class="post-metadata">

**Author:** ![mons.reseland](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@mons.reseland](https://forum.dynamobim.com/u/mons.reseland)\
**Post date:** [December 8, 2020, 6:06am UTC](https://forum.dynamobim.com/t/filter-elements-by-different-condition-input-by-user/58080/3 "2020-12-08T06:06:05Z")

</div>

I can post the .dyn file and the Excel file. i cannot post the RVT file unfortunately  
[Filter fra excel.dyn](https://forum.dynamobim.com/uploads/short-url/e1gWMj3uZMz7OS0CKVpVjHjtBCq.dyn) (140.1 KB) [Link element filter.xlsx](https://forum.dynamobim.com/uploads/short-url/oNwv1r0JSs4Nl8coWIWs4Dloiga.xlsx) (20.1 KB)

---

<div class="post-metadata">

**Author:** ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)\
**Post date:** [December 8, 2020, 6:19am UTC](https://forum.dynamobim.com/t/filter-elements-by-different-condition-input-by-user/58080/4 "2020-12-08T06:19:14Z")

</div>

hello @mons.reseland

there are some syntaxe problems in your code, here an example with fixes  
_remember to post your code with the \<\> tags it will help those who will help you_

```
if metode == "N/V": 
	resultat = elementer 
elif metode == "E": 
	resultat = elementer == val
elif metode == "C": 
	resultat = val in elementer
elif metode == "BW": 
	resultat = elementer.startswith(val) 
elif metode == "EW": 
	resultat = elementer.endswith(val)
else:
	resultat = False
```

---

<div class="post-metadata">

**Author:** ![mons.reseland](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@mons.reseland](https://forum.dynamobim.com/u/mons.reseland)\
**Post date:** [December 9, 2020, 3:39pm UTC](https://forum.dynamobim.com/t/filter-elements-by-different-condition-input-by-user/58080/5 "2020-12-09T15:39:45Z")

</div>

Hi @c.poupin  
Here is my original code:

\<elementer = IN[0]  
metode = IN[1]  
val = IN[2]

resultat = [“none”]

if metode == “N/V”:  
resultat = elementer  
if metode == “E”:  
if elementer.equals(val):  
resultat = True  
else resultat = false  
if metode == “C”:  
if elementer.contains(val):  
resultat = True  
else resultat = false  
if metode == “BW”:  
if elementer.startswith(val):  
resultat = True  
else resultat = false  
if metode == “EW”:  
if elementer.endswith(val):  
resultat = True  
else resultat = false

OUT = resultat\>

I tried what you said, but i only got false out of my python node.

 ![rzzyXSKkcZ](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/1/f/1fd32865fc9d63de3bb9567905af7cf8e617c7d3.png)

---

<div class="post-metadata">

**Author:** ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)\
**Post date:** [December 9, 2020, 7:53pm UTC](https://forum.dynamobim.com/t/filter-elements-by-different-condition-input-by-user/58080/6 "2020-12-09T19:53:38Z")

</div>

Hello,  
an Example

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/5/0/50a65f4baba1cc4b9ddf47ae86295f99ac0979a1.jpeg)

---

<div class="post-metadata">

**Author:** ![mons.reseland](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@mons.reseland](https://forum.dynamobim.com/u/mons.reseland)\
**Post date:** [December 10, 2020, 10:05am UTC](https://forum.dynamobim.com/t/filter-elements-by-different-condition-input-by-user/58080/7 "2020-12-10T10:05:45Z")

</div>

@c.poupin the script does not recognize that the list of elements is a list of lists. I have to make the script look in the different sublist for all the elements that match the inputs by the user and return a true or flse value, from there i can filter out the elements with a filter bool mask. kind of like this

 ![2ROec3b0du](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/2/7/27f9f0a772772f4796650682b25d77d9ec670081.png)

do i perhaps need to do a “for loop” or something?

---

<div class="post-metadata">

**Author:** ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)\
**Post date:** [December 10, 2020, 4:03pm UTC](https://forum.dynamobim.com/t/filter-elements-by-different-condition-input-by-user/58080/8 "2020-12-10T16:03:20Z")

</div>

Hello  
try this (the code is to be adapted if ever there are more conditions)

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/6/5/65c0a28659f9a14a6a25df9cf13f8a8576b5cc22.png)

```
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

def getDataParameter(lstmetode, lstval):
	for idx, (metode, val) in enumerate(zip(lstmetode, lstval)):
		if metode is not None and val is not None:
			return idx, metode, val
	
	
lstelementer = IN[0]
lstmetode = IN[1] 
lstval = IN[2] 
outCheck = []
idx, metode, val = getDataParameter(lstmetode, lstval)
for elementer in lstelementer[idx]:
	if metode == "N/V": 
		resultat = elementer 
	elif metode == "E": 
		resultat = elementer == val
	elif metode == "C": 
		resultat = val in elementer
	elif metode == "BW": 
		resultat = elementer.startswith(val) 
	elif metode == "EW": 
		resultat = elementer.endswith(val)
	else:
		resultat = False
	outCheck.append(resultat)
	
OUT = outCheck
```

---

<div class="post-metadata">

**Author:** ![mons.reseland](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@mons.reseland](https://forum.dynamobim.com/u/mons.reseland)\
**Post date:** [December 11, 2020, 9:06am UTC](https://forum.dynamobim.com/t/filter-elements-by-different-condition-input-by-user/58080/9 "2020-12-11T09:06:16Z")

</div>

Hello @c.poupin  
Awesome, thank you, almost there 🙂 I think that when the metode equals N/V the result should be true, because then the script passes all the elements to the next evaluation. So i see that now the script does not keep the list structure, to the next part, it only evaluate the first sublist. I need it to be adaptive so i can add as many lines in my excel sheet as i want and the script will evaluate all the elements in each sublist by that sublists condition, if that makes sense 🙂 Here is a picture of how it works now.

 ![V5oepaQGZZ](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/7/8/78e070deea1a2b9013d571570d2f97a6e7288033.png)

I also modified the scrip a little, but i did not get the result i wanted

```
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

def getDataParameter(lstmetode, lstval):
	for idx, (metode, val) in enumerate(zip(lstmetode, lstval)):
		if metode is not None and val is not None:
			return idx, metode, val
	
	
lstelementer = IN[0]
lstmetode = IN[1] 
lstval = IN[2] 
outCheck = []
idx, metode, val = getDataParameter(lstmetode, lstval)
for elementer in lstelementer[idx]:
	if metode == "N/V": 
		resultat = True
	elif metode == "E": 
		resultat = elementer == val
	elif metode == "C": 
		resultat = val in elementer
	elif metode == "BW": 
		resultat = elementer.startswith(val) 
	elif metode == "EW": 
		resultat = elementer.endswith(val)
	else:
		resultat = False
	outCheck.append(resultat)
	
OUT = outCheck
```

---

<div class="post-metadata">

**Author:** ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)\
**Post date:** [December 11, 2020, 11:34pm UTC](https://forum.dynamobim.com/t/filter-elements-by-different-condition-input-by-user/58080/10 "2020-12-11T23:34:05Z")

</div>

> [@c.poupin](#):
>
> (the code is to be adapted if ever there are more conditions)

using zip function

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/8/9/89698fa7649fa78edc6c57ec631857e2ef806d72.jpeg)

---

<div class="post-metadata">

**Author:** ![mons.reseland](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@mons.reseland](https://forum.dynamobim.com/u/mons.reseland)\
**Post date:** [December 14, 2020, 11:42am UTC](https://forum.dynamobim.com/t/filter-elements-by-different-condition-input-by-user/58080/11 "2020-12-14T11:42:10Z")

</div>

Thank you so much @c.poupin! your code, it worked beautifully 🙂  
I have learned so much

 ![lF9yB5p4DK](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/8/0/80efaadff2f225bef4446f8aeedc83e02dc101dc.png)
