Python itertools.product freezes with higher numbers
<p>I have 6 variables with different ranges. I want to create possibilities pool with my code. In this example i gave 10 range for every variable but i have to give them about 200 range. But whenever i'm trying to exceed 20 range (for example 30 range) Python kills itself, and sometimes it freezes computer. Is there anyway to make it faster and stable?</p>

<p>Thanks.</p>

<pre><code>import itertools

a = [x for x in range(400,411)]
b = [x for x in range(400,411)]
c = [x for x in range(400,411)]
d = [x for x in range(400,411)]
e = [x for x in range(400,411)]
f = [x for x in range(400,411)]

fl = lambda x: x

it = filter(fl, itertools.product(a,b,c,d,e,f))

posslist = [x for x in it]

print(len(posslist))
</code></pre>

<p>Maybe instead of doing all of it one-go, you can try doing the product in successive steps. For example,</p>

<pre><code>import itertools as itt

a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8, 9]

fl = lambda x: x

s1 = filter(fl, itt.product(a, b))
s2 = filter(fl, itt.product(s1, c))

print(list(s2))
</code></pre>

<p>But then the result would look like the following. You just have to unpack the tuple of tuple to a single tuple.</p>

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

<p>I also think that this can be made parallelizable where you can do the product of the lists in parallel.</p>

<p>For lists L1, L2, L3, L4, do something like:</p>

<pre><code>th1_res = itertools.product(L1, L2)
th2_res = itertools.product(L3, L4)

thread_final = itertools.product(th1_res, th2_res)
</code></pre>

python python-3.x itertools