Generate combinations (in specific order) of all permutations of the dict of lists
<p>I have a dict of lists</p>

<pre><code>d = {'A': [1,2,3], 'B': [4,5], 'C': [6]}
</code></pre>

<p>I need to produce all permutation of each list (A, B and C). This is OK.</p>

<pre><code>p = {}
for k in d.keys():
    p[k] = [i for i in itertools.permutations(d[k])]
</code></pre>

<p>This results in <code>p</code></p>

<pre><code>{'A': [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)],
 'B': [(4, 5), (5, 4)],
 'C': [(6,)]}
</code></pre>

<p>Then I need to merge tuples from A, B and C lists but in the specific order (for example in order of <code>sorted(p.keys())</code> which actually gives <code>['A', 'B', 'C']</code>). So I should obtain list of tuples of integers:</p>

<pre><code>[(1,2,3,4,5,6),
 (1,2,3,5,4,6),
 (1,3,2,4,5,6),
 (1,3,2,5,4,6),
 ...
 (3,2,1,5,4,6)
]
</code></pre>

<p>I know that <code>itertools.product</code> could be used in such cases, but the initial dictionary <code>d</code> can consist of arbitrary number of values with different keys and I don't know how to use it in this case.
Or maybe you will be able to suggest totally different solution of the desribed problem. The faster the final solution will work the better.</p>
