Find if an element is unique in the list

#1

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.

0 Likes

#2

List.uniqueitems i believe

0 Likes

#3

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

0 Likes

#4

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.

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

0 Likes

#5

But combining that and list.contains? :wink:

0 Likes

#6

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:

0 Likes

#7

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

0 Likes

#8

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

0 Likes

#9

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

0 Likes

#10

sample.dyn (13.7 KB)

0 Likes

#11

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
1 Like

#12

It works. Thank you very much.

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

0 Likes

#13

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

0 Likes

#14

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

0 Likes

#15

I see. Try this instead.

0 Likes