Not sure if it`s pseudocode or python?
Since you posted it in the grasshooper section , maybe you want to move it or change the title so it mentions python.
not sure if I am not completly misunderstanding you.
Most of the components have a flatten option when you right click over the input. Besides there exists a flatten component.
Well… in this case the list is already flattened. I’m sure your solution could work in some situations but sometimes tree structure is also important. Boundary surface sorts curves by Z Much more useful in my opinion would be to preserve input structure…
here’s it is on a simple contour on some boxes example…