Merge lists in specific order
<p>I have a list of lists and i want to merge the lists with an specific order. See example:</p>

<pre><code>id list  0         1           2
       [[0], [2, 6, 1, 4], [3, 7, 5]]

  Order          Resulting List
[1, 0, 2] = [2, 6, 1, 4, 0, 3, 7, 5]
[0, 2, 1] = [0, 3, 7, 5, 2, 6, 1, 4]
[2, 1, 0] = [3, 7, 5, 2, 6, 1, 4, 0]
</code></pre>

<p>Someone can suggest a more elegant algorithm that proposed below?</p>

<pre><code>    groups = [[0], [2, 6, 1, 4], [3, 7, 5]]
    orders = [[1, 0, 2], [0, 2, 1], [2, 1, 0]]

    for order in orders:
        LC = []
        for i in order:
            LC += groups[i]
    return LC
</code></pre>

<p>Let me explain a bit better what I need:</p>

<pre><code>groups = [[0], [2, 6, 1, 4], [3, 7, 5]] 
orders = [[0, 2, 1], [1, 0, 2], [2, 1, 0]] # Order of each group in LC
solutions = [] # I want to put the created LC here
for order in orders:
    LC = [] # I need this because a want LCs individualy and not one with all 
    for i in order: # for each order I pick de index (i) of the group
         LC += grupos[i] # and merge then according with index of group
        solutions.append([LC])
    print(solutions)
</code></pre>

<p>I want this (one LC for each order):</p>

<pre><code>[[0, 3, 7, 5, 2, 6, 1, 4], [2, 6, 1, 4, 0, 3, 7, 5], [3, 7, 5, 2, 6, 1, 4, 0]]
</code></pre>

<p>and not this:</p>

<pre><code>[0, 3, 7, 5, 2, 6, 1, 4, 2, 6, 1, 4, 0, 3, 7, 5, 3, 7, 5, 2, 6, 1, 4, 0]
</code></pre>

<p>The algorithm above works, but a need a another one more elegant and efficient.</p>

<p>Some examples of output:</p>

<pre><code>groups = [[0], [2, 1], [3, 7, 5], [4], [6]]

Order = [1, 0, 2, 3, 4]
LC = [2, 1, 0, 3, 7, 5, 4, 6]

    [2, 1, 0, 3, 4]
    [3, 7, 5, 2, 1, 0, 4, 6]

    [3, 1, 2, 0, 4]
    [4, 2, 1, 3, 7, 5, 0, 6]

    [4, 1, 2, 3, 0]
    [6, 2, 1, 3, 7, 5, 4, 0]

    [0, 2, 1, 3, 4]
    [0, 3, 7, 5, 2, 1, 4, 6]

    [0, 3, 2, 1, 4]
    [0, 4, 3, 7, 5, 2, 1, 6]

    [0, 4, 2, 3, 1]
    [0, 6, 3, 7, 5, 4, 2, 1]

    [0, 1, 3, 2, 4]
    [0, 2, 1, 4, 3, 7, 5, 6]

    [0, 1, 4, 3, 2]
    [0, 2, 1, 6, 4, 3, 7, 5]

    [0, 1, 2, 4, 3]
    [0, 2, 1, 3, 7, 5, 6, 4]
</code></pre>

<pre><code>inputs = [[1,2,3], [4,5,6], [7]]
orders = [[0,1,2], [2,1,0]]

result = [input_element for order in orders for order_element in order for input_element in inputs[order_element]]
print(result)
</code></pre>

python list merge