# List replace by condition to Empty List Item

**URL:** <https://forum.dynamobim.com/t/list-replace-by-condition-to-empty-list-item/68516>\
**Category:** Revit\
**Tags:** revit, python, dynamo\
**Created:** [September 23, 2021, 5:18pm UTC](https://forum.dynamobim.com/t/list-replace-by-condition-to-empty-list-item/68516 "2021-09-23T17:18:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruben.romero](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ruben.romero/32/115842_2.png) [@ruben.romero](https://forum.dynamobim.com/u/ruben.romero)\
**Post date:** [September 23, 2021, 5:18pm UTC](https://forum.dynamobim.com/t/list-replace-by-condition-to-empty-list-item/68516/1 "2021-09-23T17:18:14Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/4/6/46c2952b9491805b3064707e49780b56922abed6.png)

---

<div class="post-metadata">

**Author:** ![Marcel\_Rijsmus](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/marcel_rijsmus/32/8539_2.png) [@Marcel\_Rijsmus](https://forum.dynamobim.com/u/Marcel_Rijsmus)\
**Post date:** [September 23, 2021, 5:26pm UTC](https://forum.dynamobim.com/t/list-replace-by-condition-to-empty-list-item/68516/2 "2021-09-23T17:26:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![t.large](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/t.large/32/89412_2.png) [@t.large](https://forum.dynamobim.com/u/t.large)\
**Post date:** [September 23, 2021, 5:44pm UTC](https://forum.dynamobim.com/t/list-replace-by-condition-to-empty-list-item/68516/3 "2021-09-23T17:44:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![RubenVivancos](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/rubenvivancos/32/185795_2.png) [@RubenVivancos](https://forum.dynamobim.com/u/RubenVivancos)\
**Post date:** [September 23, 2021, 6:08pm UTC](https://forum.dynamobim.com/t/list-replace-by-condition-to-empty-list-item/68516/4 "2021-09-23T18:08:32Z")

</div>

> [@Marcel\_Rijsmus](#):
>
> the inputs

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

---

<div class="post-metadata">

**Author:** ![Marcel\_Rijsmus](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/marcel_rijsmus/32/8539_2.png) [@Marcel\_Rijsmus](https://forum.dynamobim.com/u/Marcel_Rijsmus)\
**Post date:** [September 23, 2021, 6:12pm UTC](https://forum.dynamobim.com/t/list-replace-by-condition-to-empty-list-item/68516/5 "2021-09-23T18:12:23Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [September 23, 2021, 6:17pm UTC](https://forum.dynamobim.com/t/list-replace-by-condition-to-empty-list-item/68516/6 "2021-09-23T18:17:43Z")

</div>

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:

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

---

<div class="post-metadata">

**Author:** ![t.large](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/t.large/32/89412_2.png) [@t.large](https://forum.dynamobim.com/u/t.large)\
**Post date:** [September 23, 2021, 7:53pm UTC](https://forum.dynamobim.com/t/list-replace-by-condition-to-empty-list-item/68516/7 "2021-09-23T19:53:36Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [September 23, 2021, 8:34pm UTC](https://forum.dynamobim.com/t/list-replace-by-condition-to-empty-list-item/68516/8 "2021-09-23T20:34:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ruben.romero](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ruben.romero/32/115842_2.png) [@ruben.romero](https://forum.dynamobim.com/u/ruben.romero)\
**Post date:** [September 23, 2021, 8:41pm UTC](https://forum.dynamobim.com/t/list-replace-by-condition-to-empty-list-item/68516/9 "2021-09-23T20:41:04Z")

</div>

> [@Nick\_Boyts](#):
>
> you can use this workaround:

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?

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/a/d/ad53e45a02d774c146de78a2d03c32ae7eeba01f.png)

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [September 23, 2021, 8:50pm UTC](https://forum.dynamobim.com/t/list-replace-by-condition-to-empty-list-item/68516/10 "2021-09-23T20:50:27Z")

</div>

> [@ruben.romero](#):
>
> now the problem is that I cannot flatten the list with empty lists

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.

---

<div class="post-metadata">

**Author:** ![ruben.romero](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ruben.romero/32/115842_2.png) [@ruben.romero](https://forum.dynamobim.com/u/ruben.romero)\
**Post date:** [September 23, 2021, 9:02pm UTC](https://forum.dynamobim.com/t/list-replace-by-condition-to-empty-list-item/68516/11 "2021-09-23T21:02:39Z")

</div>

> [@Nick\_Boyts](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [September 23, 2021, 9:27pm UTC](https://forum.dynamobim.com/t/list-replace-by-condition-to-empty-list-item/68516/12 "2021-09-23T21:27:45Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![ruben.romero](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ruben.romero/32/115842_2.png) [@ruben.romero](https://forum.dynamobim.com/u/ruben.romero)\
**Post date:** [September 23, 2021, 10:07pm UTC](https://forum.dynamobim.com/t/list-replace-by-condition-to-empty-list-item/68516/13 "2021-09-23T22:07:54Z")

</div>

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:

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/7/e/7e0794bc6261f27446452e2114d8801e0d314a0b.png)

---

<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:** [September 24, 2021, 3:36am UTC](https://forum.dynamobim.com/t/list-replace-by-condition-to-empty-list-item/68516/14 "2021-09-24T03:36:39Z")

</div>

@ruben.romero See if this serves your purpose

 ![empty_2021-09-24_09-04-24](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/4/2/429b6c9d859dae673b714755afbecd35e0cc07ed.png)

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

```

---

<div class="post-metadata">

**Author:** ![ruben.romero](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ruben.romero/32/115842_2.png) [@ruben.romero](https://forum.dynamobim.com/u/ruben.romero)\
**Post date:** [September 24, 2021, 3:41am UTC](https://forum.dynamobim.com/t/list-replace-by-condition-to-empty-list-item/68516/15 "2021-09-24T03:41:05Z")

</div>

> [@Vikram\_Subbaiah](#):
>
> @ruben.romero See if this serves your purpose

that is magic! awesome, many thanks

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/9/6/967d2a3f176ab4e0d213a78d7225604f77b62081.png)

---

<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:** [September 24, 2021, 3:47am UTC](https://forum.dynamobim.com/t/list-replace-by-condition-to-empty-list-item/68516/16 "2021-09-24T03:47:01Z")

</div>

> [@ruben.romero](#):
>
> that is magic! awesome, many thanks

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

---

<div class="post-metadata">

**Author:** ![Organon](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/organon/32/161404_2.png) [@Organon](https://forum.dynamobim.com/u/Organon)\
**Post date:** [September 24, 2021, 2:30pm UTC](https://forum.dynamobim.com/t/list-replace-by-condition-to-empty-list-item/68516/17 "2021-09-24T14:30:39Z")

</div>

@ruben.romero

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

```auto
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);

```

 ![RecursiveReplacement](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/6/3/63238706eb357cce370fea0181e3cae5e9002a52.png)

**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.

---

<div class="post-metadata">

**Author:** ![ruben.romero](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ruben.romero/32/115842_2.png) [@ruben.romero](https://forum.dynamobim.com/u/ruben.romero)\
**Post date:** [September 25, 2021, 1:08am UTC](https://forum.dynamobim.com/t/list-replace-by-condition-to-empty-list-item/68516/18 "2021-09-25T01:08:51Z")

</div>

> [@Organon](#):
>
> with DesignScript you can

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

---

<div class="post-metadata">

**Author:** ![Organon](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/organon/32/161404_2.png) [@Organon](https://forum.dynamobim.com/u/Organon)\
**Post date:** [September 25, 2021, 4:06pm UTC](https://forum.dynamobim.com/t/list-replace-by-condition-to-empty-list-item/68516/19 "2021-09-25T16:06:15Z")

</div>

@ruben.romero

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

Try deleting some braces.

![RecursiveEmpty](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/6/d/6df341629ad9fa9ef4c3c49cdda3df1f185f5fe7.png)

Regards,

---

<div class="post-metadata">

**Author:** ![ruben.romero](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ruben.romero/32/115842_2.png) [@ruben.romero](https://forum.dynamobim.com/u/ruben.romero)\
**Post date:** [September 25, 2021, 6:37pm UTC](https://forum.dynamobim.com/t/list-replace-by-condition-to-empty-list-item/68516/20 "2021-09-25T18:37:01Z")

</div>

> [@Vikram\_Subbaiah](#):
>
> ```auto
> def emp (a:var[]..[])
> {
> return [Imperative]
> {
> b = [];
> for (i in a)
> {
> if (i < 0)
> {
> return List.Empty;
> }
> else
> {
> return i;
> }
> }
> }
> };
> 
> ```

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

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/6/0/6044ff2e2a1b4968e3ee83319dc8292712ad132f.png)

[Next page](https://forum.dynamobim.com/t/list-replace-by-condition-to-empty-list-item/68516.md?page=2)
