# Python Lists - Maintain Dynamo List Structure

**URL:** <https://forum.dynamobim.com/t/python-lists-maintain-dynamo-list-structure/37081>\
**Category:** Developers\
**Created:** [June 13, 2019, 1:14am UTC](https://forum.dynamobim.com/t/python-lists-maintain-dynamo-list-structure/37081 "2019-06-13T01:14:05Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Dunn1](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@Michael\_Dunn1](https://forum.dynamobim.com/u/Michael_Dunn1)\
**Post date:** [June 13, 2019, 1:14am UTC](https://forum.dynamobim.com/t/python-lists-maintain-dynamo-list-structure/37081/1 "2019-06-13T01:14:05Z")

</div>

Hi - I’m very new to programming so don’t really understand what I’m doing!

In the following script, I’m taking input from a bunch of rooms (as shown in screenshot). I’d like to extract the room boundaries of each room and maintain the inputted list structure as I perform various other operations (essentially finding the overall boundary of a unit group in Revit by using the rooms in the group). I can perform everything on a single group but as soon as I am inputting multiple groups (so nested lists of rooms), I can’t get the function to work.

I found Konrad’s post about defining a function to work through lists but can’t get it to work in this instance (perhaps because the each room then gets its own nested list once the room boundaries are found?).

Any help appreciated!

```
rooms = IN[0]
rBounds = []

def ProcessList(_func, _list):
	return map(lambda x: ProcessList(_func, x) if type(x)==list else func(x), _list)

def roomBounds(room):
	return room.FinishBoundary

# get room boundaries as curves

if isinstance (rooms, list):
	for r in rooms:
			s = ProcessList(roomBounds(r), rooms)
			rBounds.append(s)

OUT = rBounds

```

This is the desired outcome:

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

And this is the python that currently works for a single list of rooms (the above is working towards getting the below function working with nested lists)

```
# The inputs to this node will be stored as a list in the IN variables.
rooms = IN[0]
offAmount = IN[1]
solids = []
plane = Plane.XY()
rBounds = []

# get room boundaries as curves
t = []
for r in rooms:
	s = r.FinishBoundary
	t.append(s)

rBounds = List.Flatten(t, 1)

#for each list of curves in input list
for a in rBounds:
	b = PolyCurve.ByJoinedCurves(a, 0.001)	#create polycurves
	c = Curve.Offset(b, offAmount)	#offset curves
	d = Surface.ByPatch(c)	#create surface
	e = Surface.Thicken(d, 10000, True)	#extrude surface (create solid)
	solids.append(e)
	
solid = Solid.ByUnion(solids) #union solids
surfaceInt = Geometry.Intersect(solid,plane) #find intersection of a plane with this solid
perimCurves = Surface.PerimeterCurves(surfaceInt[0]) #get the perimeter curves of the intersection

# Assign your output to the OUT variable.
OUT = perimCurves
```

---

<div class="post-metadata">

**Author:** ![salvatoredragotta](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/salvatoredragotta/32/10078_2.png) [@salvatoredragotta](https://forum.dynamobim.com/u/salvatoredragotta)\
**Post date:** [June 13, 2019, 1:32am UTC](https://forum.dynamobim.com/t/python-lists-maintain-dynamo-list-structure/37081/2 "2019-06-13T01:32:01Z")

</div>

@Michael_Dunn1 have you tried creating a custom node with the above Python code that works with single list of rooms ?

---

<div class="post-metadata">

**Author:** ![Michael\_Dunn1](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@Michael\_Dunn1](https://forum.dynamobim.com/u/Michael_Dunn1)\
**Post date:** [June 13, 2019, 2:25am UTC](https://forum.dynamobim.com/t/python-lists-maintain-dynamo-list-structure/37081/3 "2019-06-13T02:25:06Z")

</div>

Hi Salvatore - yes, the bottom piece of code in the post (I edited after posting) contains the code from a custom node that works with a single list.

---

<div class="post-metadata">

**Author:** ![salvatoredragotta](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/salvatoredragotta/32/10078_2.png) [@salvatoredragotta](https://forum.dynamobim.com/u/salvatoredragotta)\
**Post date:** [June 13, 2019, 2:26am UTC](https://forum.dynamobim.com/t/python-lists-maintain-dynamo-list-structure/37081/4 "2019-06-13T02:26:07Z")

</div>

@Michael_Dunn1 so can you use list List@level with multiple lists?

---

<div class="post-metadata">

**Author:** ![Michael\_Dunn1](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@Michael\_Dunn1](https://forum.dynamobim.com/u/Michael_Dunn1)\
**Post date:** [June 14, 2019, 1:33am UTC](https://forum.dynamobim.com/t/python-lists-maintain-dynamo-list-structure/37081/5 "2019-06-14T01:33:47Z")

</div>

Using List@level as the input to my node only works half way and I’m not quite sure why. As in, I should be generating 7 lists of curves but it returns null for the last 3 lists (there shouldn’t be a difference between them).

---

<div class="post-metadata">

**Author:** ![salvatoredragotta](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/salvatoredragotta/32/10078_2.png) [@salvatoredragotta](https://forum.dynamobim.com/u/salvatoredragotta)\
**Post date:** [June 14, 2019, 1:35am UTC](https://forum.dynamobim.com/t/python-lists-maintain-dynamo-list-structure/37081/6 "2019-06-14T01:35:27Z")

</div>

@Michael_Dunn1 can you provide your dyn and a dummy revit file to test?

---

<div class="post-metadata">

**Author:** ![Michael\_Dunn1](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@Michael\_Dunn1](https://forum.dynamobim.com/u/Michael_Dunn1)\
**Post date:** [June 14, 2019, 1:55am UTC](https://forum.dynamobim.com/t/python-lists-maintain-dynamo-list-structure/37081/7 "2019-06-14T01:55:38Z")

</div>

Sure - thanks!

[PerimeterCurvesFromListOfRooms.dyf (8.0 KB)](https://forum.dynamobim.com/uploads/short-url/yd74ybNKVK5WVpXgGXiRd9TaSqN.dyf) [UnitTypeKeyPlanFromGroup\_Python.dyn (212.3 KB)](https://forum.dynamobim.com/uploads/short-url/w9uPuPC1YcSNIAgyILQs9FYlHPp.dyn)

Revit file here:  
[https://1drv.ms/u/s!AplXcxKV8foBgodTWmU5sNGW0dO2yg?e=QQYGoT](https://1drv.ms/u/s!AplXcxKV8foBgodTWmU5sNGW0dO2yg?e=QQYGoT)

---

<div class="post-metadata">

**Author:** ![salvatoredragotta](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/salvatoredragotta/32/10078_2.png) [@salvatoredragotta](https://forum.dynamobim.com/u/salvatoredragotta)\
**Post date:** [June 14, 2019, 2:49am UTC](https://forum.dynamobim.com/t/python-lists-maintain-dynamo-list-structure/37081/8 "2019-06-14T02:49:13Z")

</div>

> [@Michael\_Dunn1](#):
>
> Sure - thanks!

Thanks. I can’t open your dyn! it crashed Revit 2019. What version of Dynamo are you using?

Can anyone open the dyn?

---

<div class="post-metadata">

**Author:** ![Michael\_Dunn1](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@Michael\_Dunn1](https://forum.dynamobim.com/u/Michael_Dunn1)\
**Post date:** [June 14, 2019, 4:22am UTC](https://forum.dynamobim.com/t/python-lists-maintain-dynamo-list-structure/37081/9 "2019-06-14T04:22:09Z")

</div>

Running Dynamo 2.0.3 daily build but that graph has that effect on me too (I was hoping it was just me!) Usually if I open another graph first and run it on the model (generally anything that changes a parameter value) and then open that dyn it works fine.

---

<div class="post-metadata">

**Author:** ![Kulkul](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/kulkul/32/49801_2.png) [@Kulkul](https://forum.dynamobim.com/u/Kulkul)\
**Post date:** [June 14, 2019, 6:07am UTC](https://forum.dynamobim.com/t/python-lists-maintain-dynamo-list-structure/37081/10 "2019-06-14T06:07:35Z")

</div>

Hi @Michael_Dunn1

I fixed your python Script it will work now with any list structure:

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

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

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.GeometryConversion)

ProcessLists = lambda function, lists: [ProcessLists(function, item) if isinstance(item, list) else function(item) for item in lists]
ApplyFunction = lambda func, objs: ProcessLists(func, objs) if isinstance(objs, list) else [func(objs)]

def Unwrap(item):
    return UnwrapElement(item)
    
if isinstance(IN[0], list):
	room = ProcessLists(Unwrap, IN[0])
else:
	room = Unwrap(IN[0])

def roomFinish(rooms):
	options = Autodesk.Revit.DB.SpatialElementBoundaryOptions()
	options.SpatialElementBoundaryLocation = SpatialElementBoundaryLocation.Finish
	segments = rooms.GetBoundarySegments(options) 
	lines = [i.GetCurve().ToProtoType() for i in segments[0]] 
	return lines
OUT = ApplyFunction(roomFinish,room)
```

---

<div class="post-metadata">

**Author:** ![Kulkul](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/kulkul/32/49801_2.png) [@Kulkul](https://forum.dynamobim.com/u/Kulkul)\
**Post date:** [June 14, 2019, 9:36am UTC](https://forum.dynamobim.com/t/python-lists-maintain-dynamo-list-structure/37081/11 "2019-06-14T09:36:23Z")

</div>

Hi @salvatoredragotta

> [@salvatoredragotta](#):
>
> Can anyone open the dyn?

Yes i can open without any issues. See above solution ☝

---

<div class="post-metadata">

**Author:** ![Michael\_Dunn1](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@Michael\_Dunn1](https://forum.dynamobim.com/u/Michael_Dunn1)\
**Post date:** [June 16, 2019, 9:25pm UTC](https://forum.dynamobim.com/t/python-lists-maintain-dynamo-list-structure/37081/12 "2019-06-16T21:25:34Z")

</div>

Thank you @Kulkul! I’ll give that a go and hopefully understand the solution. 🙂

---

<div class="post-metadata">

**Author:** ![Kulkul](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/kulkul/32/49801_2.png) [@Kulkul](https://forum.dynamobim.com/u/Kulkul)\
**Post date:** [June 17, 2019, 10:19am UTC](https://forum.dynamobim.com/t/python-lists-maintain-dynamo-list-structure/37081/13 "2019-06-17T10:19:58Z")

</div>

> [@Michael\_Dunn1](#):
>
> I’ll give that a go and hopefully understand the solution

@Michael_Dunn1 Don’t forget to mark the post as solved.
