# Loop a list inside a loop list

**URL:** <https://forum.dynamobim.com/t/loop-a-list-inside-a-loop-list/104321>\
**Category:** Revit\
**Tags:** revit, python, dynamo\
**Created:** [September 13, 2024, 7:38am UTC](https://forum.dynamobim.com/t/loop-a-list-inside-a-loop-list/104321 "2024-09-13T07:38:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fc.castro113](https://avatars.discourse-cdn.com/v4/letter/f/49beb7/32.png) [@fc.castro113](https://forum.dynamobim.com/u/fc.castro113)\
**Post date:** [September 13, 2024, 7:38am UTC](https://forum.dynamobim.com/t/loop-a-list-inside-a-loop-list/104321/1 "2024-09-13T07:38:08Z")

</div>

Hello everyone!

I would like to know if there is a way to compare every element on a list with the elements of another list. If the comparisson matches, it should retrieve an element. The part of the script that i have its already working but by taking one of lists index, but istead, i would like it to do it by a loop.

Here’s the part of the code that its already working

rmLst = IN[0]

### Seleccionar habitaciones

rmsTyp = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST\_Rooms).WhereElementIsNotElementType().ToElements()

### Filtrar habitaciones por lista

rmsWR =

for rms in rmsTyp :  
rmNm = rms.get\_Parameter(BuiltInParameter.ROOM\_NAME).AsString()  
if (rmNm == rmLst[0]) or (rmNm == rmLst[1]) :  
rmsWR.append(rms)

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

---

<div class="post-metadata">

**Author:** ![Draxl\_Andreas](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/draxl_andreas/32/145744_2.png) [@Draxl\_Andreas](https://forum.dynamobim.com/u/Draxl_Andreas)\
**Post date:** [September 13, 2024, 10:03am UTC](https://forum.dynamobim.com/t/loop-a-list-inside-a-loop-list/104321/2 "2024-09-13T10:03:01Z")

</div>

@fc.castro113

here your code pasted correctly \</\>

```py
import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('System')
from System.Collections.Generic import List

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

# Access the Revit application and document
app = DocumentManager.Instance.CurrentUIApplication
doc = DocumentManager.Instance.CurrentDBDocument

rmLst = IN[0]

# Seleccionar habitaciones
rmsTyp = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Rooms).WhereElementIsNotElementType().ToElements()

# Filtrar habitaciones por lista
rmsWR = []

for rms in rmsTyp :
    rmNm = rms.get_Parameter(BuiltInParameter.ROOM_NAME).AsString()
    if (rmNm == rmLst[0]) or (rmNm == rmLst[1]) :
        rmsWR.append(rms)

```

---

<div class="post-metadata">

**Author:** ![Draxl\_Andreas](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/draxl_andreas/32/145744_2.png) [@Draxl\_Andreas](https://forum.dynamobim.com/u/Draxl_Andreas)\
**Post date:** [September 13, 2024, 10:20am UTC](https://forum.dynamobim.com/t/loop-a-list-inside-a-loop-list/104321/3 "2024-09-13T10:20:52Z")

</div>

@fc.castro113

in dynamo you can use dictionary

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/2/e/2ed6e01d1fca24226b10823b5fd0ad9670e8b931.png)

i just added to the code a OUT

it works too

```py
import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('System')
from System.Collections.Generic import List

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

# Access the Revit application and document
app = DocumentManager.Instance.CurrentUIApplication
doc = DocumentManager.Instance.CurrentDBDocument

room_names_desired_list = IN[0]

# Seleccionar habitaciones
rooms = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Rooms).WhereElementIsNotElementType().ToElements()

# Filtrar habitaciones por lista

rooms_name_match = []

for room in rooms:
    room_name = room.get_Parameter(BuiltInParameter.ROOM_NAME).AsString()
    if (room_name == room_names_desired_list[0]) or (room_name == room_names_desired_list[1]) :
        rooms_name_match.append(room)
        
OUT = rooms_name_match

```

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [September 13, 2024, 3:52pm UTC](https://forum.dynamobim.com/t/loop-a-list-inside-a-loop-list/104321/4 "2024-09-13T15:52:48Z")

</div>

It would be helpful to know exactly what you’re doing and where your values are coming from. If you have a list of names and you want to get the associated room based on a specific order, then a dictionary is best.

If you have a list of names and you want to know if your list of rooms match those names then you can just check for matches by confirming the room name is in the list of names like this:

```auto
for rms in rmsTyp:
    if rms.get_Parameter(BuiltInParameter.ROOM_NAME).AsString() in rmLst:
        rmsWR.append(rms)

```

Your only input is a list of names to match and use an OR condition for the matching so I’m guessing your use case is the one above. If you want the rooms in the same order as the named list then you just need to loop through that list first (as an alternative to the dictionary method).

```auto
for rmLstNm in rmLst:
    for rms in rmsTyp:
        if rms.get_Parameter(BuiltInParameter.ROOM_NAME).AsString() == rmLstNm:
            rmsWR.append(rms)

```

---

<div class="post-metadata">

**Author:** ![fc.castro113](https://avatars.discourse-cdn.com/v4/letter/f/49beb7/32.png) [@fc.castro113](https://forum.dynamobim.com/u/fc.castro113)\
**Post date:** [September 18, 2024, 7:06am UTC](https://forum.dynamobim.com/t/loop-a-list-inside-a-loop-list/104321/5 "2024-09-18T07:06:14Z")

</div>

Thanks sorry i didnt know how to post it correctly

---

<div class="post-metadata">

**Author:** ![fc.castro113](https://avatars.discourse-cdn.com/v4/letter/f/49beb7/32.png) [@fc.castro113](https://forum.dynamobim.com/u/fc.castro113)\
**Post date:** [September 18, 2024, 7:11am UTC](https://forum.dynamobim.com/t/loop-a-list-inside-a-loop-list/104321/6 "2024-09-18T07:11:51Z")

</div>

Well, actually i dont know if a dictionary could work. Lets say that i have a large number of rooms and i just want to pick the ones named “toilet” so i can work with those later. Yes this code works like that, and its similar as the one i post (sorry i didnt post it complete), but i would like to know if this part of the code:

if (room\_name == room\_names\_desired\_list[0]) or (room\_name == room\_names\_desired\_list[1]) :

could be replaced by a list of elements so i dont have to input every element separetly

Thanks a lot for ur replay

---

<div class="post-metadata">

**Author:** ![fc.castro113](https://avatars.discourse-cdn.com/v4/letter/f/49beb7/32.png) [@fc.castro113](https://forum.dynamobim.com/u/fc.castro113)\
**Post date:** [September 18, 2024, 7:26am UTC](https://forum.dynamobim.com/t/loop-a-list-inside-a-loop-list/104321/7 "2024-09-18T07:26:58Z")

</div>

@Nick_Boyts the second solution works perfect. You were right, the values that i have are a number of rooms retrieved from revit and a list of names which i wanted to use as a filter to select the rooms that matches those names. Thanks a lot for your solution!
