# Null to Empty to Variable

**URL:** <https://forum.dynamobim.com/t/null-to-empty-to-variable/68560>\
**Category:** Developers\
**Tags:** revit, python, dynamo\
**Created:** [September 25, 2021, 4:23am UTC](https://forum.dynamobim.com/t/null-to-empty-to-variable/68560 "2021-09-25T04:23:23Z")\
**Posts on this page:** 10\
**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 25, 2021, 4:23am UTC](https://forum.dynamobim.com/t/null-to-empty-to-variable/68560/1 "2021-09-25T04:23:23Z")

</div>

Hello,

I have to ask some frequent tasks that I would like to achieve with OOTB nodes or DesignScript, although I ended up using Python nodes but they do not get to work input as a lists&sublists and not wanting to insert them in a custom node to run with Long lacing mode.

I have seen some people using Imperative Design script or something called Replication guides, not far from the Python codes,

Here what I got so far working with, looking to improve, any suggestion would be appreciated, thanks.

**Null to Empty:**

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

inpo = IN[0]
result = []

if isinstance(inpo, (list,)):
	for each in inpo:
		if each == None:
			result.append([])
		else:
			result.append(each)
elif inpo == None:
	result = []
else:
	result = inpo
OUT = result

```

**Empty to Variable** :

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

inpo = IN[0]
string = IN[1]
result = []

if isinstance(inpo, (list,)):
	for each in inpo:
		if each == []:
			result.append(string)
		else:
			result.append(each)
elif inpo == []:
	result = None
else:
	result = inpo
OUT = result

```

---

<div class="post-metadata">

**Author:** ![GavinNicholls](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gavinnicholls/32/190442_2.png) [@GavinNicholls](https://forum.dynamobim.com/u/GavinNicholls)\
**Post date:** [September 25, 2021, 4:51am UTC](https://forum.dynamobim.com/t/null-to-empty-to-variable/68560/2 "2021-09-25T04:51:44Z")

</div>

I use this node in all my python scripts to ensure lists are received, even if only one element is provided:

```auto
def tolist(input):
    result = input if isinstance(input, list) else [input]
    return result

```

You can apply similar logic to the first item in a list using try/except statements to force lists of a certain depth to be available, but yes as you said otherwise you will need either imperative design script or custom nodes to make your life easier. I ended up going with custom nodes in the end both for myself (Crumple) and for some of my clients with inhouse packages. It’s just so much easier having level and lacing inputs available rather than making complex Python or imperative statements in most cases.

---

<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, 4:58am UTC](https://forum.dynamobim.com/t/null-to-empty-to-variable/68560/3 "2021-09-25T04:58:13Z")

</div>

> [@GavinNicholls](#):
>
> in most cases

I agree but for that simple task like a boolean or replacement I think is unnecessary any levels/lacing as I always expect the same output behaviour

---

<div class="post-metadata">

**Author:** ![GavinNicholls](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gavinnicholls/32/190442_2.png) [@GavinNicholls](https://forum.dynamobim.com/u/GavinNicholls)\
**Post date:** [September 25, 2021, 4:59am UTC](https://forum.dynamobim.com/t/null-to-empty-to-variable/68560/4 "2021-09-25T04:59:39Z")

</div>

Time to learn imperative design script then I guess. I saw you got some help from Vikram on another thread about similar things - check out his post history and there’ll be plenty of examples.

---

<div class="post-metadata">

**Author:** ![EdsonMatt](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/edsonmatt/32/124725_2.png) [@EdsonMatt](https://forum.dynamobim.com/u/EdsonMatt)\
**Post date:** [September 25, 2021, 3:58pm UTC](https://forum.dynamobim.com/t/null-to-empty-to-variable/68560/5 "2021-09-25T15:58:33Z")

</div>

Hi @ruben.romero ,

I’m not sure about what you’re looking for, but is something like this?

 ![1](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/8/6/8606f7b88bb3200ec266d0ce858212969f1f40dd.jpeg)

```auto
def item_tolist (input):
	tolist = input if isinstance(input,list) else [input]
	return tolist

var = item_tolist(IN[0])
result = []

for item in var:
	if item==None:
		result.append([])
	else:
		result.append(item)
	
OUT = result

```

```auto
def item_tolist (input):
	tolist = input if isinstance(input,list) else [input]
	return tolist

var = item_tolist(IN[0])
string = item_tolist(IN[1])
result = []

for item in var:
	if item==[]:
		result.append(string)
	else:
		result.append(item)

OUT = result

```

---

<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:20pm UTC](https://forum.dynamobim.com/t/null-to-empty-to-variable/68560/6 "2021-09-25T18:20:56Z")

</div>

> [@EdsonMatt](#):
>
> ```auto
> def item_tolist (input):
> tolist = input if isinstance(input,list) else [input]
> return tolist
> 
> var = item_tolist(IN[0])
> string = item_tolist(IN[1])
> result = []
> 
> for item in var:
> if item==[]:
> result.append(string)
> else:
> result.append(item)
> 
> OUT = result
> 
> ```

I get same identical list before the python script, it does not work for some reason

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/4/9/4948fcec37a6da3220137e289b36f64cc1a68f5e.png)

---

<div class="post-metadata">

**Author:** ![EdsonMatt](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/edsonmatt/32/124725_2.png) [@EdsonMatt](https://forum.dynamobim.com/u/EdsonMatt)\
**Post date:** [September 25, 2021, 8:57pm UTC](https://forum.dynamobim.com/t/null-to-empty-to-variable/68560/7 "2021-09-25T20:57:14Z")

</div>

It didn’t work because you have a list with sub-lists, which means that you have to add another loop to iterate the sub-lists as well. So this would be something like:

 ![1](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/9/a/9ab2767df905db8757346a5af06c22fd21928dce.jpeg)

```auto
def item_tolist (input):
	tolist = input if isinstance(input,list) else [input]
	return tolist

var = item_tolist(IN[0])
string = IN[1]

result = []
first_lp_list = []

for lst in var:
	if lst == []:
		first_lp_list.append(string)
		result.append(first_lp_list)
	else:
		sec_lp_list = []
		for item in lst:
			if item==[]:
				sec_lp_list.append(string)
			else:
				sec_lp_list.append(item)
		result.append(sec_lp_list)
	
OUT = result

```

You can also check @AmolShah solution for a similar case.

> [@List.IsNotEmpty fails](https://forum.dynamobim.com/t/list-isnotempty-fails/68345):
>
> Greeting. I have a list of boolean which is input to List.Filter. First problem I encountered is List.IsNotEmpty come out as Null. And it made ListFilter fails. As I guess the reason come from the null data, I tried to replace the Null by EmptyList. Second problem I encountered is the List.NullToEmpty fails by telling me Value cannot be null. I have no idea why these happened.

---

<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:** [September 26, 2021, 9:32am UTC](https://forum.dynamobim.com/t/null-to-empty-to-variable/68560/8 "2021-09-26T09:32:57Z")

</div>

Another solution with a python recursive function

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/4/0/40149efaa113cdd69885bd43dee6bca2e70708b8.png)

---

<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 26, 2021, 3:13pm UTC](https://forum.dynamobim.com/t/null-to-empty-to-variable/68560/9 "2021-09-26T15:13:20Z")

</div>

> [@c.poupin](#):
>
> Another solution with a python recursive function

I tried to modify the script of @c.poupin to get the results needed, I like that solution easy to edit and runs pretty well. As a resume:

**Null to Empty** List I get it resolved finally like this:

```auto
import sys
def recurseReplace(lst, replace):
	global funcEval
	newlst = lst[:]
	for idx, elem in enumerate(lst):
		if isinstance(elem, list) and len(elem) > 0:
			newlst[idx] = recurseReplace(lst[idx], replace)
		else:
			if funcEval(lst[idx]):
				newlst[idx] = replace
			else:
				continue
	return newlst

nestedlist = IN[0]
funcEval = lambda x : x is None
NulltoEmpty = recurseReplace(nestedlist, replace = [])

OUT = NulltoEmpty

```

Also combined double condition of **Empty List to String and Nulls to String** in a single script with 1 input list and 2 inputs strings, which is something I was looking at the end as well.

```auto
import sys
def recurseReplace(lst, replace):
	global funcEval
	newlst = lst[:]
	for idx, elem in enumerate(lst):
		if isinstance(elem, list) and len(elem) > 0:
			newlst[idx] = recurseReplace(lst[idx], replace)
		else:
			if funcEval(lst[idx]):
				newlst[idx] = replace
			else:
				continue
	return newlst

nestedlist = IN[0]
funcEval = lambda x : x is x == []
EmptytoString = recurseReplace(nestedlist, replace = IN[1])
funcEval = lambda x : x is None
Nulltostring = recurseReplace(EmptytoString, replace = IN[2])

OUT = Nulltostring

```

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

Many thanks for all your support @EdsonMatt @GavinNicholls @Organon @Vikram_Subbaiah , I tested all the solutions provided and learnt something new.

---

<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 26, 2021, 4:49pm UTC](https://forum.dynamobim.com/t/null-to-empty-to-variable/68560/10 "2021-09-26T16:49:32Z")

</div>

I think it’s time to find your own solution. You already have a lot of good solutions shared by different users. You need to get your hands dirty, instead of adding more requirements for your work, and expect someone else to do it for you.
