Hello
Im trying to get a list that have the same structure as the parent list with all unique entries.
To clarify more, the below picture has a parent list (26 entries) consisting of 4 sub-lists:
the third sub-list has the last entry as “15” and is duplicated in the 4th sub-list. what I’m trying to accomplish is this (keep drop the nth entry whenever it is duplicated):
25 entries by dropping the duplicate in the 4th list.
I’ve tried using indexing like this:
but i couldn’t get anywhere.
any help is much appreciated.
Hi @NERDASORUS
Here this might be of help:
list_of_lists = IN[0]
# Step 1: Flatten the list of lists
flattened_list = [item for sublist in list_of_lists for item in sublist]
# Step 2: Use a set to identify unique elements
unique_elements = set(flattened_list)
# Step 3: Create a new list of lists with unique elements only
unique_list_of_lists = []
seen_elements = set()
for sublist in list_of_lists:
unique_sublist = []
for item in sublist:
if item in unique_elements and item not in seen_elements:
unique_sublist.append(item)
seen_elements.add(item)
if unique_sublist:
unique_list_of_lists.append(unique_sublist)
# Output the result
OUT = unique_list_of_lists
Just open a python node and paste this code as it is, it’ll help you get exactly what you trying to do.
regards,
BM
1 Like
Instead of checking if the last item is repeated, you could just ensure no member of a sub-list repeats in a subsequent one
unique.dyn (13.9 KB)
5 Likes