Is there a pythonic way of permuting a list of list?
<p>I have a list of lists containing unique strings and I want to produce an arbitrary number of different ways of sorting it. The list might look like the following:</p>

<pre><code>list = [[a], [b,c], [d], [e,f,g]]
</code></pre>

<p>The order of the lists need to be the same but I want to shuffle the ordering within a list and then have them in a single list, e.g</p>

<pre><code>list1 = [a,b,c,d,e,f,g]
list2 = [a,c,b,d,f,e,g]
...
...
listN = [a,c,b,d,f,g,e]
</code></pre>

<p>What is a good pythonic way of achieving this? I'm on python 2.7.</p>

<p>This might not be the most elegant solution, but I think it does what you want</p>

<pre><code>from itertools import permutations
import numpy as np

def fac(n):
    if n&lt;=1:
        return 1
    else:
        return n * fac(n-1)

lists = [['a'], ['b','c'], ['d'], ['e','f','g']]

combined = [[]]
for perm in [permutations(l,r=len(l)) for l in lists]:
    expanded = []
    for e in list(perm):
        expanded += [list(l) + list(e) for l in combined]
    combined = expanded

## check length

print np.prod(map(fac,map(len,lists))), len(combined)

print '\n'.join(map(str,combined))
</code></pre>

python list python-2.7 permutation