List unique in nested

Hi, is it possible to list the unique items of this list but keep the list structure?

How do you want it to look?

I was about to delete. AI sorted me out

def get_unique_values(nested_list):
    seen = set()

    def recursive_unique(sublist):
        if isinstance(sublist, list):
            new_sublist = []
            for item in sublist:
                if isinstance(item, list):
                    new_sublist.append(recursive_unique(item))
                elif item not in seen:
                    seen.add(item)
                    new_sublist.append(item)
            return new_sublist
        return sublist

    return recursive_unique(nested_list)

# Input list from Dynamo
nested_list = IN[0]

# Get unique values while preserving structure
unique_values = get_unique_values(nested_list)

# Output to Dynamo
OUT = unique_values

1 Like

Always nicer to share an answer though. :slight_smile:

List.UniqueItems with the input set to @@L2 should work here as well. And no Python to maintain.

1 Like

AI just discovered Depth First Search! You can ace your coding interview now :wink:

1 Like

Encase theres anything you want to look into with the list.unique

Wait you want to discard the subsequent sublists? If is likely best to keep your Python as while I believe Unique works for nested lists you can’t replace the items with an empty lust like this.

What is the use case here?

Sorting curves for correct dimension placements. This catches some overlaying ones from kick plates but keeps correct groups encase of a vision panel placement.

1 Like

Yipes! Feels like there is an easier way to do this via the initial collection…

A DS function to avoid Replication Guide complexity for nested lists

def removeSubsequent (lst:var[]..[])
{
	lst01 = List.TakeItems(lst,1..List.Count(lst)-1);
	lst02 = List.SetDifference(List.RestOfItems(lst)<1>,List.Flatten(lst01<1>,-1)<1>);
	return  List.AddItemToFront(List.UniqueItems(List.FirstItem(lst)),lst02);
};

removeSubsequent (lst<1>);
4 Likes

I’m not sure if this is the best way but 2min of a fresh head got me a better solution then 3 hours of a tired head.

Doors I’m testing on


Current solution. Not sure I want to be diming glazed door frames but I should be able to sort that.