Is there any way to generate list exponentially in Python?
<p>I have a dictionary:</p>

<p>D = {1:[1,2,3], 2:[4,5], 3: [6,7]}</p>

<p>What I wish to do is to find all 3*2*2 combinations,</p>

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

<p>Is there any way, just doing loop like</p>

<pre><code>for key in D:
   for num in D[key]:
     for xxxxx
</code></pre>

<p>and then carry out the all combination? Thanks! </p>

<p>Use <a href="https://docs.python.org/3/library/itertools.html#itertools.product"><strong><code>itertools.product</code></strong></a>:</p>

<pre><code>itertools.product(*D.values())
</code></pre>

<p>Example:</p>

<pre><code>&gt;&gt;&gt; import itertools
&gt;&gt;&gt; D = {1:[1,2,3], 2:[4,5], 3: [6,7]}
&gt;&gt;&gt; list(itertools.product(*D.values()))
[(1, 4, 6), (1, 4, 7), (1, 5, 6), (1, 5, 7), (2, 4, 6), (2, 4, 7),
 (2, 5, 6), (2, 5, 7), (3, 4, 6), (3, 4, 7), (3, 5, 6), (3, 5, 7)]
</code></pre>

python list hashtable