# Flatten list with python

**URL:** <https://forum.dynamobim.com/t/flatten-list-with-python/55642>\
**Category:** Developers\
**Tags:** python\
**Created:** [September 28, 2020, 1:41pm UTC](https://forum.dynamobim.com/t/flatten-list-with-python/55642 "2020-09-28T13:41:53Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alien](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/alien/32/40701_2.png) [@Alien](https://forum.dynamobim.com/u/Alien)\
**Post date:** [September 28, 2020, 1:41pm UTC](https://forum.dynamobim.com/t/flatten-list-with-python/55642/1 "2020-09-28T13:41:54Z")

</div>

Me again 😆

I’m trying to flatten a list in python, I’ve tried it several ways:

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

Then I checked these forums and found this method but it doesn’t work for me:

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

I’m feeding in a list of points and I’d like the list flattened so the python looks like the List.Flatten node.

> >

Traceback (most recent call last):  
File “”, line 10, in   
TypeError: Point is not iterable\<\< Was the error from the 2nd screenshot.

How do I do this?

---

<div class="post-metadata">

**Author:** ![mellouze](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@mellouze](https://forum.dynamobim.com/u/mellouze)\
**Post date:** [September 28, 2020, 3:35pm UTC](https://forum.dynamobim.com/t/flatten-list-with-python/55642/2 "2020-09-28T15:35:16Z")

</div>

Have you tried this ? [https://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists](https://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists)

In your case, it should translate this way :

```
import collections

def flattenList(unflattened_list):
	if isinstance(unflattened_list,collections.Iterable):
		return [sub_element for element in unflattened_list for sub_element in flattenList(element)]
	else:
		return [unflattened_list]
OUT = flattenList(IN[0])

```

_Edit :_ Worked for me outside of Dynamo :

 ![2020-09-28 17_38_26-C__Python27_python.exe](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/a/7/a7542ea458d585413eacbf4c7b49f707845bfedc.png)

---

<div class="post-metadata">

**Author:** ![Alien](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/alien/32/40701_2.png) [@Alien](https://forum.dynamobim.com/u/Alien)\
**Post date:** [September 28, 2020, 3:43pm UTC](https://forum.dynamobim.com/t/flatten-list-with-python/55642/3 "2020-09-28T15:43:55Z")

</div>

Oooo… Thank you!

I’d done a work around (I am actually generating the list within the script so I flattened the first bit then added to it) but this is much nicer. 🙂

---

<div class="post-metadata">

**Author:** ![solamour](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/solamour/32/153516_2.png) [@solamour](https://forum.dynamobim.com/u/solamour)\
**Post date:** [September 28, 2020, 10:29pm UTC](https://forum.dynamobim.com/t/flatten-list-with-python/55642/4 "2020-09-28T22:29:09Z")

</div>

It has some caveats (i.e. cannot be ran with numbers unless inside a real list), but this also works using _List Comprehensions_

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/8/8/88c01ab202583813525e9929ac07c7530e68dd0c.png)

```auto
lst = IN[0]

def flat(l):
    return [item for sublist in l for item in sublist]

OUT = flat(lst)
```

Or even just a List Comprehension itself

```auto
OUT = [item for sublist in IN[0] for item in sublist]
```

---

<div class="post-metadata">

**Author:** ![Alien](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/alien/32/40701_2.png) [@Alien](https://forum.dynamobim.com/u/Alien)\
**Post date:** [September 29, 2020, 8:08am UTC](https://forum.dynamobim.com/t/flatten-list-with-python/55642/5 "2020-09-29T08:08:41Z")

</div>

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

> > Traceback (most recent call last):  
> > File “”, line 15, in   
> > File “”, line 13, in flat  
> > TypeError: iteration over non-sequence of type Point\<\<

Same warning error for the other one too.

I tried loads of variations of loops and stuff and none worked.  
mellouze’s solution does though… If anyone has an explanation why I’d be interested as I’m obviously still a total noob.

---

<div class="post-metadata">

**Author:** ![mellouze](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@mellouze](https://forum.dynamobim.com/u/mellouze)\
**Post date:** [September 29, 2020, 12:06pm UTC](https://forum.dynamobim.com/t/flatten-list-with-python/55642/6 "2020-09-29T12:06:07Z")

</div>

I’ll try to explain both functions with words.

```
def flat(l):
    return [item for sublist in l for item in sublist]

```

- Take `l` as entry (`l` is supposed to be a list)
- Return a list constructed as such : An enumeration of all the `item` contained in all the `sublist` of `l`.

Here, each object named `sublist` is required to be an iterable (e.g. a list). If called, the function will try to iterate over each `sublist` item. In your case, you are feeding a list `l` that contains both points and lists of points. The function will successfully iterate on all the lists of points, but will return an error when it will try to iterate on the points (which is the error you quote in your last post).

```
def flattenList(l):
	if isinstance(l,collections.Iterable):
		return [item for sublist in l for item in flattenList(sublist)]
	else:
		return [l]

```

Here, the function is a bit more complex. (I changed the name of the variables to match the one of the previous function)

- Given a variable `l`, the function will first check if it is an instance of `collections.Iterable`, which essentially dictates if Python will be able to iterate over it.
- If the object `l` is an iterable (meaning that in our case, it is a list), the function will return something similar to @solamour’s function. However, Python will not iterate over `sublist` but over `flattenList(sublist)` : as you may notice, the function calls itself. In that case, Python will first apply `flattenList` to `sublist`, then will continue to iterate.
- If the object `l` is not an iterable (e.g. it’s a Point), Python will return a list composed only of `l`.

If you are not familiar with recursive functions, try reading this : [https://www.programiz.com/python-programming/recursion](https://www.programiz.com/python-programming/recursion)

---

<div class="post-metadata">

**Author:** ![Alien](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/alien/32/40701_2.png) [@Alien](https://forum.dynamobim.com/u/Alien)\
**Post date:** [September 29, 2020, 12:18pm UTC](https://forum.dynamobim.com/t/flatten-list-with-python/55642/7 "2020-09-29T12:18:26Z")

</div>

Thanks mellouze!

I’ve only been learning python since May, I’ve come across recursive functions but my knowledge is still pretty shaky. In fact I’m still struggling with functions in general. 😖

I love this forum, with every post I’m learning. 🕶

---

<div class="post-metadata">

**Author:** ![AmolShah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/amolshah/32/103244_2.png) [@AmolShah](https://forum.dynamobim.com/u/AmolShah)\
**Post date:** [September 29, 2020, 1:34pm UTC](https://forum.dynamobim.com/t/flatten-list-with-python/55642/8 "2020-09-29T13:34:17Z")

</div>

Just curious to why we would import collections? 🤔  
Can we not change the code to use isinstance(l, **list** ) instead?

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

---

<div class="post-metadata">

**Author:** ![mellouze](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@mellouze](https://forum.dynamobim.com/u/mellouze)\
**Post date:** [September 29, 2020, 1:50pm UTC](https://forum.dynamobim.com/t/flatten-list-with-python/55642/9 "2020-09-29T13:50:56Z")

</div>

> [@mellouze](#):
>
> Have you tried this ? [https://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists](https://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists)

To be honest, I only copied the answer that was given here.  
I guess that in this case, `isintance(l,list)` is sufficient, but if you would want to flatten iterables that are not list, you would have to use `isinstance(l,collections.Iterable)`.

---

<div class="post-metadata">

**Author:** ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)\
**Post date:** [September 29, 2020, 2:22pm UTC](https://forum.dynamobim.com/t/flatten-list-with-python/55642/10 "2020-09-29T14:22:14Z")

</div>

> [@AmolShah](#):
>
> Just curious to why we would import collections? 🤔  
> Can we not change the code to use isinstance(l, **list** ) instead?

because all iterables are not necessarily simple Python lists

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

---

<div class="post-metadata">

**Author:** ![AmolShah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/amolshah/32/103244_2.png) [@AmolShah](https://forum.dynamobim.com/u/AmolShah)\
**Post date:** [September 29, 2020, 2:25pm UTC](https://forum.dynamobim.com/t/flatten-list-with-python/55642/11 "2020-09-29T14:25:41Z")

</div>

@mellouze Now that makes sense. I was just thinking about the points case!  
@c.poupin Thanks for the example

---

<div class="post-metadata">

**Author:** ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)\
**Post date:** [September 29, 2020, 2:43pm UTC](https://forum.dynamobim.com/t/flatten-list-with-python/55642/12 "2020-09-29T14:43:17Z")

</div>

Another example with a recursive generator

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

---

<div class="post-metadata">

**Author:** ![ruben.romero](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ruben.romero/32/115842_2.png) [@ruben.romero](https://forum.dynamobim.com/u/ruben.romero)\
**Post date:** [June 8, 2022, 12:11pm UTC](https://forum.dynamobim.com/t/flatten-list-with-python/55642/13 "2022-06-08T12:11:46Z")

</div>

> [@solamour](#):
>
> ```auto
> lst = IN[0]
> 
> def flat(l):
> return [item for sublist in l for item in sublist]
> 
> OUT = flat(lst)
> 
> ```

In case there are sublists inside sublist and I want to flatten to the first sublist, how that would be?  
the result asked would be like the input list of this example, with OOTB nodes would be flatten to list level 2 but no idea in python.

for example:

inputlist=[[[1,2,3],0,2,3],[[1,2,3],1,2,3],[1,2,[3,2,1]]]  
outputlist=[[1,2,3,0,2,3],[1,2,3,1,2,3],[1,2,3,2,1]]
