# Flatten nested list while keeping the strucutre

**URL:** <https://forum.dynamobim.com/t/flatten-nested-list-while-keeping-the-strucutre/31084>\
**Category:** Developers\
**Created:** [January 23, 2019, 10:17am UTC](https://forum.dynamobim.com/t/flatten-nested-list-while-keeping-the-strucutre/31084 "2019-01-23T10:17:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![saeedy3](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@saeedy3](https://forum.dynamobim.com/u/saeedy3)\
**Post date:** [January 23, 2019, 10:17am UTC](https://forum.dynamobim.com/t/flatten-nested-list-while-keeping-the-strucutre/31084/1 "2019-01-23T10:17:15Z")

</div>

Hi all,

Trying to flatten a nested list while keeping the structure of the list in python, how do I do it properly?

In essence, what I am trying to achieve in python script is what I am getting in List.Flatten.  
see graph below for details:

 ![flatten%20nested%20list](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/2/7/271b9c26e2150e98792c2b6d34370450f667f2e9.jpeg)

---

<div class="post-metadata">

**Author:** ![Thomas\_Corrie](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/thomas_corrie/32/63060_2.png) [@Thomas\_Corrie](https://forum.dynamobim.com/u/Thomas_Corrie)\
**Post date:** [January 23, 2019, 10:25am UTC](https://forum.dynamobim.com/t/flatten-nested-list-while-keeping-the-strucutre/31084/2 "2019-01-23T10:25:12Z")

</div>

Try using amt of 1 on the List.Flatten node to remove the top layer:

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

---

<div class="post-metadata">

**Author:** ![saeedy3](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@saeedy3](https://forum.dynamobim.com/u/saeedy3)\
**Post date:** [January 23, 2019, 10:30am UTC](https://forum.dynamobim.com/t/flatten-nested-list-while-keeping-the-strucutre/31084/3 "2019-01-23T10:30:33Z")

</div>

Hi @Thomas_Corrie thank you for your answer, just edited the question to clarify what I am seeking. Basically, I got the answer in list.flatten node, however, I can’t mangae to get the same result from python node. So the answer I am after should be in python.

But again thanks for taking the time to solve it 🙂

---

<div class="post-metadata">

**Author:** ![Thomas\_Corrie](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/thomas_corrie/32/63060_2.png) [@Thomas\_Corrie](https://forum.dynamobim.com/u/Thomas_Corrie)\
**Post date:** [January 23, 2019, 10:31am UTC](https://forum.dynamobim.com/t/flatten-nested-list-while-keeping-the-strucutre/31084/4 "2019-01-23T10:31:20Z")

</div>

I see, I thought you wanted the other way around 🙂

---

<div class="post-metadata">

**Author:** ![saeedy3](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@saeedy3](https://forum.dynamobim.com/u/saeedy3)\
**Post date:** [January 23, 2019, 10:32am UTC](https://forum.dynamobim.com/t/flatten-nested-list-while-keeping-the-strucutre/31084/5 "2019-01-23T10:32:57Z")

</div>

@Thomas_Corrie thanks

---

<div class="post-metadata">

**Author:** ![Thomas\_Corrie](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/thomas_corrie/32/63060_2.png) [@Thomas\_Corrie](https://forum.dynamobim.com/u/Thomas_Corrie)\
**Post date:** [January 23, 2019, 10:47am UTC](https://forum.dynamobim.com/t/flatten-nested-list-while-keeping-the-strucutre/31084/6 "2019-01-23T10:47:54Z")

</div>

My Python skills are limited but how about this? There’s almost certainly a more Pythonic way to do it

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/7/9/792ffbb67df22fc94f47faa8f5857aeaa7c4fa90.png)

```
# Enable Python support and load DesignScript library
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

# The inputs to this node will be stored as a list in the IN variables.
list1 = IN[0]

outlist = []
for x in list1:
	outlist.append([])
	for y in x:
		if type(y) is list:
			for z in y:
				outlist[-1].append(z)
		else:
			outlist[-1].append(y)

# Place your code below this line

# Assign your output to the OUT variable.
# OUT = flattenlist(list2)
OUT = outlist
```

---

<div class="post-metadata">

**Author:** ![saeedy3](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@saeedy3](https://forum.dynamobim.com/u/saeedy3)\
**Post date:** [January 23, 2019, 10:53am UTC](https://forum.dynamobim.com/t/flatten-nested-list-while-keeping-the-strucutre/31084/7 "2019-01-23T10:53:43Z")

</div>

Thank you  
@Thomas_Corrie fantastic answer!

Your skills are quite advanced in python 😅

---

<div class="post-metadata">

**Author:** ![Thomas\_Corrie](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/thomas_corrie/32/63060_2.png) [@Thomas\_Corrie](https://forum.dynamobim.com/u/Thomas_Corrie)\
**Post date:** [January 23, 2019, 10:57am UTC](https://forum.dynamobim.com/t/flatten-nested-list-while-keeping-the-strucutre/31084/8 "2019-01-23T10:57:19Z")

</div>

@saeedy3 you’re welcome, and thank you! That solution works for your particular level of nesting but if you had some deeper lists then it would be better to have a recursive function to allow for more flexibility. But if your data is in that consistent structure then this should work well.
