Selecting vertices loop, then collapse them, but problem with indices order

On an extruded shape (flatten) along a path model, I want to roll back an retrieve the extrusion path.



I select at first the upper vertices loop, and then want to run a script. Which collapse the selected vertices, select next loop, collapse the vertices, and so on.

I’ve make a test, but I’m stucked with the vertices index, that’s not in order from top to bottom in the mesh. Sometimes the vertices are in reversed order. And the script collapse the wrong loop.

So I suppose, that I’m not using the correct method! What can be the correct process to do that in a python script?

Thanks in advance for your help,

Maelvon