How to Clean NULL items of list of sublists in Python?

Very simple question that I did not achieve after many attempts perhaps because playing with sublists.

I just want to clean the nulls of a list of sublists, it works great with OOTB node and package nodes but I do not make it work in python, any idea? the sublist is composed by elements with the green ids

Here a simple example:

The python node used tried to clean the list but nothing happened:

def ClearList(_list):
    out = []
    for _list1 in _list:
        if _list1 is None:
            continue
        if isinstance(_list1, list):
             _list1 = ClearList(_list1)
             if not _list1:
                 continue
        out.append(_list1)
    return out


OUT = ClearList(Views)

@RubenVivancos hi,

Try to change continue in pass.

Hey Ruben, could try this:

import sys
import clr

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)


# Input 
Elementlist = []

if isinstance(IN[0],list):
    Elementlist = UnwrapElement(IN[0])
else:
    Elementlist = [UnwrapElement(IN[0])]

# Code 
Outlist = []
    
for Elist in Elementlist:
    Outlist.append(filter(None, Elist))
        
OUT = Outlist

Works for Nested Lists
Greetings

1 Like

I replaced continue by pass and I am getting same output

I tried it but output is still same, very strange thing…

Can you show the rest of the code?
Is the view an element type?

1 Like

I think it is a list of elements, if I make the list flatten then the cleaning of nulls works but no idea with sublists.

@RubenVivancos ,

Please share the rest of the python code.

Hi,
seems to be an issue related to the OUT Dynamo Wrapper
@RubenVivancos see this post

1 Like

I want to think it is the reason, my question to you if wanting to reuse this code can I delete just the current document variables and give as input my current script list of Elements?

import sys
import clr
import System
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

#doc = DocumentManager.Instance.CurrentDBDocument
#uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
		
ueWrapper = next((m for m in clr.GetClrType(Revit.Elements.ElementWrapper)\
				.GetMethods() if m.Name == "Wrap"), None)
				
if ueWrapper is None:
	raise Exception("Error Method", "method 'Revit.Elements.ElementWrapper.Wrap' not found") 

#views = FilteredElementCollector(doc).OfClass(View).ToElements()

outlist = []

for i in views:
	iswrapable = Revit.Elements.ElementWrapper.Wrap(i, True)
	if iswrapable is None :
		outlist.append(ueWrapper.Invoke(None, (i, True) ))
	else:
		outlist.append(i)

OUT = outlist

you need to put and adapt the code inside your python node (at the end )

share your Python script if you want

I got this warning after adding that code: Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed. Traceback (most recent call last): File "<string>", line 140, in <module> TypeError: expected LinePatternElement, got List[Element]

I can’t help you with just this message

1 Like

This means that somewhere in your code you’re passing a list, but it expects an LinePatternElement. Output your data type before that issue so you can see if you need to pull something out of a list or initiate a loop.

1 Like

“List Clean” node use from “GeniusLoci” package.

The output is still the same in this case with the function code:

def ClearList(primList):
    result = []
    for sublist in primList:
        if sublist is "":
            continue
        elif sublist is None:
            continue 
        if isinstance(sublist, list):
             sublist = ClearList(sublist)
             if not sublist:
                 continue
        result.append(sublist)
    return result

OUT = ClearList(Views)

The issue is that the null is not coming from the python list. It’s coming from the conversion to Dynamo. It looks like you’re gathering all views, likely including some 3D View Templates, in which case Dynamo will fail to convert those views and return nulls instead. So you actually have to filter out 3D View Templates, not nulls, from your list of python views.

2 Likes

So is possible to make it as a list and clean the nulls afterwards? Why the solution of @c.poupin does not work? why the clean OOTB node or any other can clean nulls?

Can you post your code or the last 10 -20 lines ?

It looks like his solution does work. Like you said, the ootb nodes will clean the nulls in the list. The issue is that your list doesn’t actually have any nulls in python. Hence, removing nulls in python will still output a list with nulls.