fPython : Making a new list from a random list of letters
<pre><code>letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']#alphabet
bag_o_letters = []#letters to chose from
letter_count = [9, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, 1]#random indexes to chose from
for x in range(26):#loops through the random index of letter_count
    for i in range(letter_count[x]):#chooses the index
        bag_o_letters.append(letters[x])#appends the index of the letter to bag_o_letters
        rack = []#list for the person to see
        for a in range(7):#makes the list 7 letters long
           rack.append(bag_o_letters.pop(random.randint(0,len(letters)-1)))#appends the letter to rack(supposedly...)
print(rack)
</code></pre>

<p>In this code that you just read it should choose random letters and put 7 of those letters in a rack that the person can see. It shows a error that I've looked over many times, but I just can't see what is wrong.</p>

<blockquote>
  <p>I put comments on the side to understand the code.</p>
</blockquote>

<p><strong>It shows this error:</strong></p>

<pre><code>rack.append(bag_of_letters.pop(random.randint(0,len(letters)-1)))
IndexError: pop index out of range
</code></pre>

<blockquote>
  <p>Can someone please help?</p>
  
  <p>After this code, I am going to make a input statement for the user to make a word from those letters.</p>
</blockquote>

<p>You're selecting the <em>index</em> to <code>pop</code> for <code>bag_of_letters</code> from the length of <code>letters</code> which is obviously larger.</p>

<p>You should instead do:</p>

<pre><code>rack.append(bag_of_letters.pop(random.randint(0, len(bag_of_letters)-1)))
#                                                    ^^^^^^^^^^^^^^
</code></pre>

<hr>

<p>However, there are likely to be more problems with your code. I'll suggest you use <a href="https://docs.python.org/2/library/random.html#random.sample" rel="nofollow"><code>random.sample</code></a> in one line of code or <a href="https://docs.python.org/2/library/random.html#random.shuffle" rel="nofollow"><code>random.shuffle</code></a> on a copy of the list, and then <em>slice</em> up till index 7. Both will give you 7 randomly selected letters:</p>

<pre><code>import random

print(random.sample(letters, 7))
# ['m', 'u', 'l', 'z', 'r', 'd', 'x']
</code></pre>

<hr>

<pre><code>import random

letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
letters_copy = letters[:]
random.shuffle(letters_copy)
print(letters_copy[:7])
# ['c', 'e', 'x', 'b', 'w', 'f', 'v']
</code></pre>

python list random