Getting previous index values of a python list items after shuffling
<p>Let's say I have such a python list:</p>

<pre><code>l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
</code></pre>

<p>by using <code>random.shuffle</code>, </p>

<pre><code>&gt;&gt;&gt; import random
&gt;&gt;&gt; random.shuffle(l)
&gt;&gt;&gt; l
[5, 3, 2, 0, 8, 7, 9, 6, 4, 1]
</code></pre>

<p>I am having the above list.</p>

<p>How can I get the previous index values list of each item in the shuffled list? </p>

<p>You could pair each item with its index using <code>enumerate</code>, then shuffle that.</p>

<pre><code>&gt;&gt;&gt; import random
&gt;&gt;&gt; l = [4, 8, 15, 16, 23, 42]
&gt;&gt;&gt; x = list(enumerate(l))
&gt;&gt;&gt; random.shuffle(x)
&gt;&gt;&gt; indices, l = zip(*x)
&gt;&gt;&gt; l
(4, 8, 15, 23, 42, 16)
&gt;&gt;&gt; indices
(0, 1, 2, 4, 5, 3)
</code></pre>

<p>One advantage of this approach is that it works regardless of whether <code>l</code> contains duplicates.</p>

python