How to remove a random element from a list and add it to another list in python
<p><code>list = ['john','james','michael','david','william']</code></p>

<p><code>winner = []</code></p>

<p>How can I remove a random item from <code>list</code> and add it to <code>winner</code>?</p>

<pre><code>winner.append(list.pop(random.randrange(0,len(list))))
</code></pre>

<p>To break this down:</p>

<pre><code>random.randrange(0,len(list)) 
</code></pre>

<p>will generate a random number between zero and the length of your list inclusive. This will generate a random index in your list that you can reference.</p>

<pre><code>list.pop(i)
</code></pre>

<p>This will remove the item at the specified index (i) from your list.</p>

<pre><code>winner.append(x)
</code></pre>

<p>This will add an item (x) to the end of the winner list. If you want to add the item at a specific index, you can use</p>

<pre><code>winner.insert(i,x) 
</code></pre>

<p>with i being the index to insert at and x being the value to insert.</p>

<p>If you want more information, a good reference is the python docs on data structures:
<a href="https://docs.python.org/2/tutorial/datastructures.html" rel="nofollow">https://docs.python.org/2/tutorial/datastructures.html</a></p>

python list random-access