# Compare if the elements from one list exist in another

**URL:** <https://forum.dynamobim.com/t/compare-if-the-elements-from-one-list-exist-in-another/37463>\
**Category:** Lists-Logic\
**Created:** [June 21, 2019, 11:52am UTC](https://forum.dynamobim.com/t/compare-if-the-elements-from-one-list-exist-in-another/37463 "2019-06-21T11:52:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Asier\_de\_Celis](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/asier_de_celis/32/79960_2.png) [@Asier\_de\_Celis](https://forum.dynamobim.com/u/Asier_de_Celis)\
**Post date:** [June 21, 2019, 11:52am UTC](https://forum.dynamobim.com/t/compare-if-the-elements-from-one-list-exist-in-another/37463/1 "2019-06-21T11:52:20Z")

</div>

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](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/1/7/173024652cbb3c69970570ea2d2d93cd5557895e.png)

---

<div class="post-metadata">

**Author:** ![mrkevinht91](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/mrkevinht91/32/72179_2.png) [@mrkevinht91](https://forum.dynamobim.com/u/mrkevinht91)\
**Post date:** [June 21, 2019, 12:46pm UTC](https://forum.dynamobim.com/t/compare-if-the-elements-from-one-list-exist-in-another/37463/2 "2019-06-21T12:46:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Asier\_de\_Celis](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/asier_de_celis/32/79960_2.png) [@Asier\_de\_Celis](https://forum.dynamobim.com/u/Asier_de_Celis)\
**Post date:** [June 21, 2019, 12:46pm UTC](https://forum.dynamobim.com/t/compare-if-the-elements-from-one-list-exist-in-another/37463/3 "2019-06-21T12:46:52Z")

</div>

Yes that is the idea

---

<div class="post-metadata">

**Author:** ![mrkevinht91](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/mrkevinht91/32/72179_2.png) [@mrkevinht91](https://forum.dynamobim.com/u/mrkevinht91)\
**Post date:** [June 21, 2019, 12:55pm UTC](https://forum.dynamobim.com/t/compare-if-the-elements-from-one-list-exist-in-another/37463/4 "2019-06-21T12:55:21Z")

</div>

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).

 ![PNG](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/2/8/28e2accf4c64ac4cf702586d377556d796ee5b69.jpeg)

---

<div class="post-metadata">

**Author:** ![Asier\_de\_Celis](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/asier_de_celis/32/79960_2.png) [@Asier\_de\_Celis](https://forum.dynamobim.com/u/Asier_de_Celis)\
**Post date:** [June 21, 2019, 12:56pm UTC](https://forum.dynamobim.com/t/compare-if-the-elements-from-one-list-exist-in-another/37463/5 "2019-06-21T12:56:57Z")

</div>

Thank you mrkevinht91 . I will try it

---

<div class="post-metadata">

**Author:** ![jonathanatger](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jonathanatger/32/49697_2.png) [@jonathanatger](https://forum.dynamobim.com/u/jonathanatger)\
**Post date:** [June 21, 2019, 1:08pm UTC](https://forum.dynamobim.com/t/compare-if-the-elements-from-one-list-exist-in-another/37463/6 "2019-06-21T13:08:06Z")

</div>

Hi Asier,

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

 ![Capture](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/1/3/1307177e6109a09a4b6b82cb79abdfe5bc573426.png)

---

<div class="post-metadata">

**Author:** ![Asier\_de\_Celis](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/asier_de_celis/32/79960_2.png) [@Asier\_de\_Celis](https://forum.dynamobim.com/u/Asier_de_Celis)\
**Post date:** [June 24, 2019, 9:32am UTC](https://forum.dynamobim.com/t/compare-if-the-elements-from-one-list-exist-in-another/37463/7 "2019-06-24T09:32:53Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/d/4/d46d4efe825e28242b9b9350d0cb47f85f0b3cc6.png)

Do you know what can it be ?

Thank you very much for your help

---

<div class="post-metadata">

**Author:** ![Asier\_de\_Celis](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/asier_de_celis/32/79960_2.png) [@Asier\_de\_Celis](https://forum.dynamobim.com/u/Asier_de_Celis)\
**Post date:** [June 24, 2019, 9:37am UTC](https://forum.dynamobim.com/t/compare-if-the-elements-from-one-list-exist-in-another/37463/8 "2019-06-24T09:37:12Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/4/7/47bfc04c5256d7e46365212c998d303cdf143947.png)

Do you know what can the problem be ?

Thank you very much for your help

---

<div class="post-metadata">

**Author:** ![stillgotme](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@stillgotme](https://forum.dynamobim.com/u/stillgotme)\
**Post date:** [June 24, 2019, 9:42am UTC](https://forum.dynamobim.com/t/compare-if-the-elements-from-one-list-exist-in-another/37463/9 "2019-06-24T09:42:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Asier\_de\_Celis](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/asier_de_celis/32/79960_2.png) [@Asier\_de\_Celis](https://forum.dynamobim.com/u/Asier_de_Celis)\
**Post date:** [June 24, 2019, 9:58am UTC](https://forum.dynamobim.com/t/compare-if-the-elements-from-one-list-exist-in-another/37463/10 "2019-06-24T09:58:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jonathanatger](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jonathanatger/32/49697_2.png) [@jonathanatger](https://forum.dynamobim.com/u/jonathanatger)\
**Post date:** [June 24, 2019, 10:43am UTC](https://forum.dynamobim.com/t/compare-if-the-elements-from-one-list-exist-in-another/37463/11 "2019-06-24T10:43:05Z")

</div>

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

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/f/1/f1b05dcd08a0009a57f3e564cdd93277609ac1c4.png)

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

---

<div class="post-metadata">

**Author:** ![stillgotme](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@stillgotme](https://forum.dynamobim.com/u/stillgotme)\
**Post date:** [June 25, 2019, 1:03am UTC](https://forum.dynamobim.com/t/compare-if-the-elements-from-one-list-exist-in-another/37463/12 "2019-06-25T01:03:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jonathanatger](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jonathanatger/32/49697_2.png) [@jonathanatger](https://forum.dynamobim.com/u/jonathanatger)\
**Post date:** [June 25, 2019, 9:45am UTC](https://forum.dynamobim.com/t/compare-if-the-elements-from-one-list-exist-in-another/37463/13 "2019-06-25T09:45:58Z")

</div>

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)
```

---

<div class="post-metadata">

**Author:** ![mrkevinht91](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/mrkevinht91/32/72179_2.png) [@mrkevinht91](https://forum.dynamobim.com/u/mrkevinht91)\
**Post date:** [June 25, 2019, 10:45am UTC](https://forum.dynamobim.com/t/compare-if-the-elements-from-one-list-exist-in-another/37463/14 "2019-06-25T10:45:00Z")

</div>

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

 ![PNG](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/4/f/4f1a2b6c936713b42c48978c52136bc7a03f0cc2.jpeg)

---

<div class="post-metadata">

**Author:** ![T\_Pover](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/t_pover/32/19147_2.png) [@T\_Pover](https://forum.dynamobim.com/u/T_Pover)\
**Post date:** [June 25, 2019, 11:06am UTC](https://forum.dynamobim.com/t/compare-if-the-elements-from-one-list-exist-in-another/37463/15 "2019-06-25T11:06:35Z")

</div>

Dynamo has a great OOTB node for this: SetIntersection

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/a/c/acb613cfb6f1f3f83efbf95d10d9806c1623fb0a.png)

---

<div class="post-metadata">

**Author:** ![Asier\_de\_Celis](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/asier_de_celis/32/79960_2.png) [@Asier\_de\_Celis](https://forum.dynamobim.com/u/Asier_de_Celis)\
**Post date:** [June 25, 2019, 11:46am UTC](https://forum.dynamobim.com/t/compare-if-the-elements-from-one-list-exist-in-another/37463/16 "2019-06-25T11:46:16Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/6/3/63f3a0a4d82ca7fee1e6e45fbeb84943488d3f00.png)

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

---

<div class="post-metadata">

**Author:** ![Asier\_de\_Celis](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/asier_de_celis/32/79960_2.png) [@Asier\_de\_Celis](https://forum.dynamobim.com/u/Asier_de_Celis)\
**Post date:** [June 25, 2019, 12:05pm UTC](https://forum.dynamobim.com/t/compare-if-the-elements-from-one-list-exist-in-another/37463/17 "2019-06-25T12:05:41Z")

</div>

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

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/4/c/4c727508703040192bf38682f4d7875992f54b86.png)

---

<div class="post-metadata">

**Author:** ![mrkevinht91](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/mrkevinht91/32/72179_2.png) [@mrkevinht91](https://forum.dynamobim.com/u/mrkevinht91)\
**Post date:** [June 25, 2019, 2:02pm UTC](https://forum.dynamobim.com/t/compare-if-the-elements-from-one-list-exist-in-another/37463/18 "2019-06-25T14:02:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Asier\_de\_Celis](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/asier_de_celis/32/79960_2.png) [@Asier\_de\_Celis](https://forum.dynamobim.com/u/Asier_de_Celis)\
**Post date:** [June 25, 2019, 2:17pm UTC](https://forum.dynamobim.com/t/compare-if-the-elements-from-one-list-exist-in-another/37463/19 "2019-06-25T14:17:01Z")

</div>

With the correction it works perfect. Thanks mrkevinht91

---

<div class="post-metadata">

**Author:** ![rohi.tariq](https://avatars.discourse-cdn.com/v4/letter/r/a87d85/32.png) [@rohi.tariq](https://forum.dynamobim.com/u/rohi.tariq)\
**Post date:** [June 8, 2021, 4:57pm UTC](https://forum.dynamobim.com/t/compare-if-the-elements-from-one-list-exist-in-another/37463/20 "2021-06-08T16:57:56Z")

</div>

Hello Everyone, yeah, for two lists, it does works perfectly.

I have two Value  
V1 = 0.5  
V1\_list = [V1-0.01, V1-0.04]  
V2 = 0.49  
I have a list which is [V1-0.01, V1-0.04]  
I need to check whether V2 exists in V1. I mean in the list or no  
Anybody could help, please.
