List replace by condition to Empty List Item

I wonder if the Passthrough node could be used:

  • If False then spit out “[something]”, feeding Passthrough (which spits out the empty list.)
  • If True then spit out [true data]

The issue has to do with list rank and not recognizing the empty list as on object, but a list with no objects. The common workaround of combining lists and getting the list of a specific index ([trueResult,falseResult][test?0:1]) is a way to get around the issue of a single empty list or lists of varying length, but does not iterate properly through sublists. Honestly, the easiest and most straight forward solution is python, but there are ways to make it work with ootb nodes, as I’ve shown.

this my weird solution but it worked, if number is negative convert it to Empty list, but now the problem is that I cannot flatten the list with empty lists haha, any solution for that new issue?

That’s the issue with rank reduction. You can’t replace an item with an empty list while still also keeping your list structure because one is a list and one is an item.

In my first example I create a full list structure of Empty items as the original and the issue was other, if the node IF had the option to set Long Lacing would be possible as well, that can be done with custom package nodes of Crumple for example.

I’m not saying it’s impossible, I’m saying it’s specifically not possible with the If node and other functions that attempt to replace items in a similar way. Dynamo has to assume a rank to iterate over when given a list. It cannot handle both lists (empty or otherwise) and items at the same rank when applying a function. This is why your current solution is forcing your other values into a list (of a singular item).

1 Like

well I do not care, just want to see results. I got other workaround more stupid than before but I get exact result as needed,I can replace negative number by Null and replace those Null by Empty List:

@ruben.romero See if this serves your purpose

def emp (a:var[]..[])
{
	return [Imperative]
	{
		b = [];
		for (i in a)
		{
			if (i < 0)
			{
				return List.Empty;
			}
			else
			{
				return i;
			}
		}
	}
};
3 Likes

that is magic! awesome, many thanks

1 Like

As I keep saying (to anyone who cares to listen), Design Script (especially replication guides) is Magic :laughing:

7 Likes

@ruben.romero

If you want to avoid using the Replication Guides, you can create a recursive function, like this one:

def RecursiveEmpty(dataList:var[]..[])
{
   return = [Imperative]
   {
      newDataList:var = []..[];
      for (i in GetKeys(dataList))
      {
		 if (List.Rank(dataList[i]) > 0)
		 {
		    newDataList[i] = RecursiveEmpty(dataList[i]);
         }
         else
         {
            if (dataList[i] < 0)
			{
			   newDataList[i] = List.Empty;
			}
			else
			{
			   newDataList[i] = dataList[i];
			}
         }
      }
      return = newDataList;
   }
};
result = RecursiveEmpty(data);

P.S. I totally agree with @Vikram_Subbaiah , with DesignScript you can do pretty much anything you want, except accessing the Revit API directly. In that case, Python comes to the rescue.

4 Likes

I am curious, with DesignScript, is possible to compress that code with few lines? too many “{ }” symbols. I have not tried the solution given by @Nick_Boyts and it may work in Designscript also

@ruben.romero

Excuse me, are you serious? I think copy and paste is too much work for you.

Try deleting some braces.

RecursiveEmpty

Regards,

6 Likes

I tried to modify it to replace Empty List by a string but not lucky, get Null results instead.

You could try this workaround
Not very clean though.

def emp (a:var[]..[])
{
	b = [Imperative]
	{
		for (i in a)
		{
			if (i==[])
			{
				return null;
			}
			else
			{
				return i;
			}
		}
	}
	return b == null ? "xx" : b;
};
2 Likes

perfect, empty to Null to variable as well

many thanks for the loads of possible solutions, finally I take this like the best option. I modified a script sample of @c.poupin of other post with similar questions to get this solution as well.

the designscript with definition in external code of @Vikram_Subbaiah gets warning telling that “emp” is not defined but it is, although I get result correct but warning and dynamo freezing, not responding for a while, in comparison the python code runs instantly without waiting response, quite tiny in the graph as well.

this kind of answer is not acceptable on a forum where you are offered help, and avoid create multiple topics with same questions

4 Likes

I tried to delete some braces but the code block gets a red warning, expecting the brace removed, I am not familiar writting code as this type, many thanks for your solution, it works well.

If you want to learn DesignScript, there is lots to download.

DesignScriptDocumentation.pdf (705.5 KB)
designscript-final.pdf (1.5 MB)
DesignScriptGuideV2.1.pdf (343.2 KB)

7 Likes