Compare if the elements from one list exist in another

Hello Everybody,

I would like to compare if the elments of one list exists in another one.
¿Is there any function or node or way for it?
image

It means you want to check whether the element of this list exists in another list ?

Yes that is the idea

You can use Python script to do it. I suggest using dictionary data structure because it will be faster to check if the element exists in the list or not (O(1) comparing to O(n) if we use array instead of dictionary).

1 Like

Thank you mrkevinht91 . I will try it

Hi Asier,

Python is always a good option. If you are not comfortable with it, here’s some nodes :

1 Like

Sorry mrkevinht91 I have tried the python solution but it is appearing the following Warning and error:

Do you know what can it be ?

Thank you very much for your help

Hello jonathanatger
I have tried with the nodes that you show in the picture but i have problem with the List contains node. It is not comparing all the elements.

Do you know what can the problem be ?

Thank you very much for your help

look at the lacing. and you should show what you typed inside the python script. not just show the shell…

I have added to the image the code inside the node.

What stillgotme points out, is that you need to set the lacing likewise :

image

This way the node performs the check for each item of the first list against each item of the second.

1 Like

To help you with this question, your x and X are of different capitalization. thats all.

That’s the first error.

But the Warning showing up is about the libraries. It is needed to add a path to the System library, this should do the trick (to be put after ‘import sys’ line 3) :

pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)
2 Likes

Maybe you don’t install python yet ? Could you try this one instead:

1 Like

Dynamo has a great OOTB node for this: SetIntersection

image

1 Like

Yes, I have alredy installed python. I think it was not able to find the path. After addiing that code, it works perfectly:

I leave the code just in case someone wants it:

import sys
pyt_path = r’C:\Program Files (x86)\IronPython 2.7\Lib’
sys.path.append(pyt_path)
import clr
clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *

from collections import Counter
list1 = IN[0]
list2 = IN[1]
x = Counter(list1)
result = [ ]
for each in list2:
if each in x:
result.append(True)
else:
result.append(False)

OUT = result

For the code of the last image. It is appearing the following warning:

you are missing “:” at line 7. It should be:
for each in set(list1):

1 Like

With the correction it works perfect. Thanks mrkevinht91

1 Like