For loop over list with designscript

I am trying to loop over list of wall elements (wall) and if the del is true (boolean) then delete.

Am I missing something?

Following is the code:

del;
wall;

[Imperative]{
still = [];

for (w in wall){
	if (del){
		Revit.Element.Delete(w);
		still.Add("Deleted!");
	}
	else{
		still.Add("Still here!");
	}

}
return = still;
};

Out of interest, why designscript and not python?

Tbh python is my go to but I thought I can do it with a simple “if condition ? {true} : {false}” but turns out more is necessary. From here to there, I continued to perform it with designscript…

1 Like

There is zero reason to move into imperative code here. Keeping it is a sing of good code.
Try this two liner instead:

objs = List.FilterByBoolMask([walls],del)["in"]
Revit.Element.Delete(objs);

The first line filters a new list containing the list of walls by the boolean ‘del’ and pulls just the stuff which passed the filter. if you provide true the one item in the list (the list of walls) will be returned. Otherwise nothing will return.

The second line deletes every object in the list of objs from the first line. If objs is an empty list no warning will trigger and nothing will happen in Revit, but if objs is a list of elements it’ll try to delete them all.

2 Likes

Thanks Jacob!
Can you elaborate on the [“in”] at the end of the filter method?

List.FilterByBoolMask is a Dynamo node you likely know of. It has multiple outputs, one for “in” which is stuff that passed the filter, and one for “out” which is stuff which didn’t pass the filter.

Typically we think of the two outputs as single items, but in reality nodes with multiple outputs return a dictionary where the port name is the key and the content of the port is the value. As such we have to call for the value of “in” from the dictionary that List.FilterByBoolMask returns, this is easiest to do with the ["in"] after the dictionary object which the node returns. You can also use the Dictionary.ValueAtKey method to parse the value, but that is a LOT more typing and you’ll get carpel tunnel by not taking the clean shortcuts which are available to you.

2 Likes