Generating a list of random lists
<p>I'm new to Python, so I might be doing basic errors, so apologies first.</p>

<p>Here is the kind of result I'm trying to obtain : </p>

<pre><code>foo = [
    ["B","C","E","A","D"],
    ["E","B","A","C","D"],
    ["D","B","A","E","C"],
    ["C","D","E","B","A"]
]
</code></pre>

<p>So basically, a list of lists of randomly permutated letters without repeat. </p>

<p>Here is the look of what I can get so far :</p>

<pre><code>foo = ['BDCEA', 'BDCEA', 'BDCEA', 'BDCEA']
</code></pre>

<p>The main problem being that everytime is the same permutation. This is my code so far :</p>

<pre><code>import random
import numpy as np
letters = ["A", "B", "C", "D", "E"]
nblines = 4
foo = np.repeat(''.join(random.sample(letters, len(letters))), nblines)  
</code></pre>

<p>Help appreciated. Thanks</p>

<p>Here's a plain Python solution using a "traditional" style <code>for</code> loop.</p>

<pre><code>from random import shuffle

nblines = 4
letters = list("ABCDE")
foo = []
for _ in range(nblines):
    shuffle(letters)
    foo.append(letters[:])

print(foo)
</code></pre>

<p><strong>typical output</strong></p>

<pre><code>[['E', 'C', 'D', 'A', 'B'], ['A', 'B', 'D', 'C', 'E'], ['A', 'C', 'B', 'E', 'D'], ['C', 'A', 'E', 'B', 'D']]
</code></pre>

<p>The <code>random.shuffle</code> function shuffles the list in-place. We append a <em>copy</em> of the list to <code>foo</code> using <code>letters[:]</code>, otherwise <code>foo</code> would just end up containing 4 references to the one list object.</p>

<hr>

<p>Here's a slightly more advanced version, using a generator function to handle the shuffling. Each time we call <code>next(sh)</code> it shuffles the <code>lst</code> list stored in the generator and returns a copy of it. So we can call <code>next(sh)</code> in a list comprehension to build the list, which is a little neater than using a traditional <code>for</code> loop. Also, list comprehesions can be slightly faster than using <code>.append</code> in a traditional <code>for</code> loop.</p>

<pre><code>from random import shuffle

def shuffler(seq):
    lst = list(seq)
    while True:
        shuffle(lst)
        yield lst[:]

sh = shuffler('ABCDE')
foo = [next(sh) for _ in range(10)]

for row in foo:
    print(row)
</code></pre>

<p><strong>typical output</strong></p>

<pre><code>['C', 'B', 'A', 'E', 'D']
['C', 'A', 'E', 'B', 'D']
['D', 'B', 'C', 'A', 'E']
['E', 'D', 'A', 'B', 'C']
['B', 'A', 'E', 'C', 'D']
['B', 'D', 'C', 'E', 'A']
['A', 'B', 'C', 'E', 'D']
['D', 'C', 'A', 'B', 'E']
['D', 'C', 'B', 'E', 'A']
['E', 'D', 'A', 'C', 'B']
</code></pre>

python