Python create list combination
<p>I have a dictionary contain element and sequence count of element. I want to create a list of list that combined from these elements</p>

<p>Example</p>

<pre><code>Input: dictElement = {"PA":2,"PB":2}

Expected Output:
[('PB0', 'PA0'),
('PB0', 'PA1'),
('PB1', 'PA0'),
('PB1', 'PA1')]
</code></pre>

<hr>

<pre><code>Input: dictElement = {"PA":2,"PB":2,"PC":3}

Expected Output:
[('PC0', 'PB0', 'PA0'), 
('PC0', 'PB0', 'PA1'), 
('PC0', 'PB1', 'PA0'), 
('PC0', 'PB1', 'PA1'), 
('PC1', 'PB0', 'PA0'), 
('PC1', 'PB0', 'PA1'), 
('PC1', 'PB1', 'PA0'), 
('PC1', 'PB1', 'PA1'), 
('PC2', 'PB0', 'PA0'), 
('PC2', 'PB0', 'PA1'), 
('PC2', 'PB1', 'PA0'), 
('PC2', 'PB1', 'PA1')]
</code></pre>

<p>Note: Number elements of dictionary can be change</p>

<p>You haven't specified in what order the keys of the dictionary should be processed in the output. If one assumes reverse sorting order, you can do this trivially with <code>itertools.product()</code>:</p>

<pre><code>from itertools import product

combinations = product(*(['{0}{1}'.format(v, i) for i in range(dictElement[v])] 
                         for v in sorted(dictElement, reverse=True))
</code></pre>

<p>Demo:</p>

<pre><code>&gt;&gt;&gt; from itertools import product
&gt;&gt;&gt; dictElement = {"PA":2,"PB":2,"PC":3}
&gt;&gt;&gt; combinations = product(*(['{0}{1}'.format(v, i) for i in range(dictElement[v])]
...                           for v in sorted(dictElement, reverse=True)))
&gt;&gt;&gt; for combo in combinations:
...     print(combo)
...
('PC0', 'PB0', 'PA0')
('PC0', 'PB0', 'PA1')
('PC0', 'PB1', 'PA0')
('PC0', 'PB1', 'PA1')
('PC1', 'PB0', 'PA0')
('PC1', 'PB0', 'PA1')
('PC1', 'PB1', 'PA0')
('PC1', 'PB1', 'PA1')
('PC2', 'PB0', 'PA0')
('PC2', 'PB0', 'PA1')
('PC2', 'PB1', 'PA0')
('PC2', 'PB1', 'PA1')
</code></pre>

python combinations