List replace by condition to Empty List Item

Hello,

I am trying to generate Empty List if a condition of a given list is met, for example number is negative.

I tried with the IF node and Empty List but result is weird. Is any way to replace a string list as a “var[]” to jump that warning?

Any solution that it is not a custom node? I am happy with Python though.

I cannot give you an answer
Let me see more
I want the whole graph or at least the inputs (preview and all) with what you feed this node
You make us guess
5 million possibilities

1 Like

What data is “true” receiving, a list of strings?

From the error message, it seems that ‘true’ is getting a string value and ‘false’ is getting the list (variable). If this is the case try throwing the data that goes to ‘true’ into a list first.

@Marcel_Rijsmus is right, in order to know for sure we need to see more than just one of the three node that is feeding the “If” node.

List of random numbers some negative other positive, bool list is true for negative numbers, and I want to replace them by empty list for each negative number and keep the others that are positive. List structure as the list of empty shown

Is that a quote?
If so, where did you get that?
If you copied that from a completely different forum question, you should be banned!

The If node cannot handle Empty Lists. Python is your best option, and probably required if you have varying list structure. Otherwise, you can use this workaround:

1 Like

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.