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>You can flatten the list then simply generate its permutations:</p>

<pre><code>from itertools import chain, permutations

li = [['a'], ['b','c'], ['d'], ['e','f','g']]
flattened = list(chain.from_iterable(li))
for perm in permutations(flattened, r=len(flattened)):
     print(perm)
&gt;&gt; ('a', 'b', 'c', 'd', 'e', 'f', 'g')
   ('a', 'b', 'c', 'd', 'e', 'g', 'f')
   ('a', 'b', 'c', 'd', 'f', 'e', 'g')
   ('a', 'b', 'c', 'd', 'f', 'g', 'e')
   ('a', 'b', 'c', 'd', 'g', 'e', 'f')
   ('a', 'b', 'c', 'd', 'g', 'f', 'e')
   ('a', 'b', 'c', 'e', 'd', 'f', 'g')
   ('a', 'b', 'c', 'e', 'd', 'g', 'f')
   ('a', 'b', 'c', 'e', 'f', 'd', 'g')
   ...
   ...
   ...
</code></pre>

python list python-2.7 permutation