Python: Flatten an irregular list of list of list


Is there any quick/easy way to flatten a list in python? I’ve searched but so far all the solutions are messy or use an external module.

Any ideas?


Hi Miguel,

Check these two topics:

Hi @djordje,

I’m looking for a small function that i could implement. I’m not working inside grasshopper, so this solutions are not really helpful.



I found a solution:

def flatten(lst):
    return sum( ([x] if not isinstance(x, list) else flatten(x) for x in lst), [] )

I found this web and it is pretty neat and helpful.


Would itertools do what you want?


Terribly sorry for that. Grasshopper seems to be on my mind.

Googling came up with this: