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

do you know how to convert that python list in a dynamo list? sounds very silly situation

It get’s converted as the output. So you can clean the nulls from the output list like Cyril showed with the List.Clean node. My point is that while you’re dealing with the list within python there are no nulls. So trying to remove nulls there will not work. The elements that end up converting to nulls are likely 3D View Templates. So if you’re trying to clean the list from within python you need to remove 3D View Templates, not nulls.

image

Try returning the view.Name instead of just the view to see what elements you’re actually dealing with. That might clear some things up.

2 Likes

so how can I ask my python list what the items are in dynamo? still same question, I think the project browser itself it is a null result in the views, not just the 3d view templates

Returning the element name as I mentioned is always a great place to start. In cases where that doesn’t help you can also return the element as a string (str(element)) to return the actual object as a readable type.

What do you mean by the ‘project browser’? Are you seeing something in Revit that makes you think there’s an issue? I may have missed something earlier in the discussion but it’s hard to tell what is going on without seeing all of your code and the objects you’re dealing with. If you can share a stripped down version of your Revit file and your python code that would be helpful as well.

EDIT: I should also ask, are you even concerned about view templates with your graph? Getting all views in the project is going to include view templates. If you don’t need view templates you could filter them out from the beginning.

1 Like

it works, I resolved like this as well, but I do not have those null items removed though, only cleans empty lists:

from itertools import chain
def unwrap_elements(elements):
    # Filter out BuiltInCategory elements
    filtered_elements = [x for x in elements if not isinstance(x, BuiltInCategory)]
    # Flatten the list of elements
    flat_list = list(chain.from_iterable(filtered_elements))
    # Remove empty lists from the list of elements
    cleaned_list = [x for x in flat_list if x]
    return cleaned_list
    
unwrapped_elements = unwrap_elements(RevitElements)

thanks @c.poupin I did not know those issues, I modified the code you shared and I think with this I resolved the issue, in my case the input list of views is a list of sublists with View Elements as items.

please let me know your thoughts as this is a very tricky one and my knowledge is too short:

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

outlistx = []

for sublist in unwrapped_elements:
    wrapped_sublist = []
    for i in sublist:
        # Check if the element is already wrapped
        if isinstance(i, Revit.Elements.ElementWrapper):
            wrapped_sublist.append(i)
        else:
            # Wrap the element if it is not already wrapped
            wrapped_element = ueWrapper.Invoke(None, (i, True))
            wrapped_sublist.append(wrapped_element)
    outlistx.append(wrapped_sublist)

OUT = outlistx

this line is not necessary:

  • Revit.Elements.ElementWrapper class has no constructor, therefore no object can inherit this class
2 Likes