In a sorted list of lists, remove elements from index number n that already exist in indexes from 0 to n-1

Hi,
I have a list of lists containing several elements. I would like to be able to go through every list and remove the elements that are already contained in the previous lists. So for example:
List with Index 0 - Does not get affected
List with Index 1 - All elements that are already in list with index 0 get removed
List with Index 2 - All elements that are already in lists with indexes 0 and 1 get removed
And so on…

image

Do I need python for this?

Python would certainly make short work of this, but you might also be able to get something with ootb nodes and a little list magic.

As for python, you would just keep a list of existing elements and check every new element against that list. If the element already exists you ignore it, otherwise it gets added back in to your output list.

Thanks @Nick_Boyts . I’ll try Python then…
I’m not very versed in Python (only in C#). However, I think the solution might lie somewhere along these lines:
# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

# Place your code below this line
ElementsList = IN[0]
outputElements = []
totalElements = []

for elements in ElementsList:
	tempElements = []
	for element in elements:
		if element not in totalElements:
			tempElements.append(element)
	totalElements.append(tempElements)
	outputElements.append(tempElements)
	
	
# Assign your output to the OUT variable.
OUT = outputElements

However, my output is the same as my input. What am I doing wrong?

You’re pretty close. Try appending the element directly to totalElements, rather than within a list. This means you’ll also want it under the same loop.

Thank you! Almost there now:

for ElementsList in ElementsLists:
	tempElements = []
	for element in ElementsList:
		if element not in totalElements:
			outputElements.append(element)
		totalElements.append(element)

	
	
# Assign your output to the OUT variable.
OUT = outputElements

image

The only problem now is that I’m breaking the list structure that I need to keep! How would I keep the original list’s structure?

That was already working. You just needed to adjust totalElements, like this:

# Place your code below this line
ElementsList = IN[0]
outputElements = []
totalElements = []

for elements in ElementsList:
	tempElements = []
	for element in elements:
		if element not in totalElements:
			tempElements.append(element)
	        totalElements.append(element)
	outputElements.append(tempElements)
	
	
# Assign your output to the OUT variable.
OUT = outputElements

silly me! working perfectly now, thank you so much!

And just for fun, here’s an ootb solution.


There are probably better ones, but this is what I came up with on a Monday morning.

That’s waaay too much magic! :grin: