Thank you for your response. However, the issue I’m encountering is within the sublists, not the main list. Additionally, I’m not very familiar with Python. Could you please assist me with this?
ChatGPT is likely going to struggle with bespoke functionality around specific use cases and would require some very specific language to describe what it is exactly you’re looking for. You can tell from looking at the code that it didn’t understand the request (it’s looking for the maximum value duplicate) and still gave bad code (it removes the item from the sublist only to add it back on the very next line).
What ChatGPT could be helpful with, and is the general exercise I’d suggest for something like this, is to break down and define all the individual parts of your condition first. See if you can:
Identify duplicate values in sublists (either by index or boolean mask).
Identify max values from each duplicate list (either by index or boolean mask).
Identify first instances of each duplicate value (either by index or boolean mask).
Cross-reference each condition for final list of duplicate values to keep and/or which to get rid of.
Python would make this much easier, but if you can work through most of the items above, we can probably help you write something with nodes that can get the trick done.
import copy
def remove_dupes(list_of_lists):
seen = set()
working_list = copy.deepcopy(list_of_lists)
for i, sub_list in enumerate(list_of_lists):
index = []
for j, val in enumerate(sub_list):
if val in seen:
index.append(j)
else:
seen.add(val)
for idx in reversed(index):
working_list[i].pop(idx)
return list(filter(None, working_list))
A few tips here
Python will modify the original object so make a deepcopy to ensure original list and sub-lists are maintained.
The pop (extracting a value) method alters a list length so if we work backwards the indexing is maintained. This may have some issues if your sublists have duplicates so we check all the values L → R and remove them in reverse.
The filter removes any empty lists - however in Python3 this is a generator so running list on it will expand the result (Dynamo also does this on processing OUT)
So as I understand it the logic is
For a list of lists
Processing each list from left to right
If the maximum number in a list is duplicated in other lists, append the other lists
import copy
def group_by_max(list_of_lists):
working_list = copy.deepcopy(list_of_lists)
result = []
while working_list:
sub_list = working_list.pop(0)
collector = [sub_list]
for i, next_list in enumerate(working_list):
if max(sub_list) in next_list:
collector.append(working_list.pop(i))
if len(collector) > 1:
result.append(collector)
else:
result.append(sub_list)
return result