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
Always nicer to share an answer though.
List.UniqueItems with the input set to @@L2 should work here as well. And no Python to maintain.
AI just discovered Depth First Search! You can ace your coding interview now
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.
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>);