Find if an element is unique in the list

Hello,
I have a list [1,1,2,3,4,4,5] and I need [false,false,true,true,false,false,true]

I went in a way where I had like 15 nodes, sorted the indices, counted them, replaced the numbers… it’s a mess. I’m sure there is an obvious easier way just can’t figure it out.

List.uniqueitems i believe

list unique item gives you the… well… unique items.
Also the list is shorter. I need a boolean with the same list structure

Hi @daninet,
See below python script. It loops the list checking whether the next or previous item are the same. It assumes your input list is ordered.

image

list = IN[0]
boolList = []

for i in range(len(list)):
	if i == 0:
		if list[i] == list[i+1]:
			boolList.append(False)
		else:
			boolList.append(True)
	elif i == len(list)-1:
		if list[i] == list[i-1]:
			boolList.append(False)
		else:
			boolList.append(True)
	else:
		if list[i] == list[i-1] or list[i] == list[i+1]:
			boolList.append(False)
		else:
			boolList.append(True)
			
#Assign your output to the OUT variable.
OUT = boolList

Cheers

But combining that and list.contains? :wink:

Hello,
Thank you, this is very close to something I want.
There are two bugs. Seems like it is not working with this sequence:
[“Ground Floor”,“Ground Floor Copy 1”,“Ground Floor”,“Ground Floor Copy 1”]
Else if there is a single element I get a null.

Can you please help with that? :beer:

Yes that was working for me with flat lists but I found it hard to do it with multi level lists.

Show don’t tell, it is immensely easier if you show what you’re having trouble with

I don’t need the views that are duplicate for my script. I’m using their names (removing a suffix part with string replace) then I need a boolean to filter out the names that are duplicate without the suffix. The views are grouped per sheet so I need to keep the list structure so I can use filterbyboolean

sample.dyn (13.7 KB)

I realize this is not optimal, but if your list will always have those two levels (sheets and views) maybe this can work:


The python script checks if each item is unique in each of the sublists.

def isUnique(item, list):
	count = 0;
	for i in list:
		if item == i:
			count += 1
	return count == 1

#The inputs to this node will be stored as a list in the IN variables.
list = IN[0]
boolList = []

for sublist in list:
	boolSublist = []
	boolList.append(boolSublist)
	for item in sublist:
		boolSublist.append(isUnique(item, sublist))
	
#Assign your output to the OUT variable.
OUT = boolList
2 Likes

It works. Thank you very much.

I wonder if it is possible to solve with core nodes with bit more flexibility

I had to use one clockwork node (list.countoccurences) but this should do what your asking with nodes only. I think.

yes I got very similar with this node, but the structure of the list is not preserved, it’s flat.
I uploaded a dyn file a little above that shows the intent clearly

I see. Try this instead.