Insert a list into another list, without brackets and by replacing the current item in that index

Viewed 4234

I'm trying to implement Hierholzers Algorithm, in Python, to find a Eulerian Cycle in a directed graph. You can find an example of the algorithm here, Hierholzers Algorithm.

In terms of the example, i have just completed the fifth stage, in other words my algorithm produces a list of levels, where each level represents a tour in the graph.
list_of_levels = [[0, 3, 2, 1, 0], [2, 6, 5, 4, 2], [6, 8, 7, 9, 6]]

To complete the process, i need to combine these lists, by inserting each level to its upper level, at the appropriate position. For example, the steps for the list above would be,
Step 1 list_of_levels = [[0, 3, 2, 1, 0], [2, 6, 8, 7, 9, 6, 5, 4, 2]]
Step 2 list_of_levels = [[0, 3, 2, 6, 8, 7, 9, 6, 5, 4, 2, 1, 0]]

So far, i have tried Python's insert(index, obj) method but the result contains the brackets of the inserted list and also it doesn't replace the item, in the index position, with the inserted item. The corresponding result with insert method, for Step 1, look like this.
[2, [6, 8, 7, 9, 6], 6, 5, 4, 2]

So the question is how to unify those levels without keeping the brackets and without ending up with duplicate items (vertices) from other levels.

I'm thinking of manually delete the vertex wherever i insert the next level and after i finish with all levels, flat the final list, although for some reason i couldn't make chain from iterable to work.

Even if i manage to implement this solution, i'm sure there is a better alternative.
I'd be happy to see some ideas.

3 Answers
Related