how to know when i CAN'T use list comprehension? : <p>so i'm in the MIT compsci class 6.00.1 and when going through the finger exercises and problems i'm trying to use list comprehension when possible, so far i've been able to.</p>

<p>from the reading that i have done it says that you can always write a <code>for</code> loop from comprehension, yet not every <code>for</code> loop can be used with comprehension.</p>

<p>i would like to know when i can't and when i can?  do i have to write the loop and then try to put it into comprehension to find out if it will work or not?  is there a way to know before i try to put it into comprehension?</p>

<p>example:</p>

<pre><code>def getAvailableLetters(lettersGuessed):

    from string import ascii_lowercase as letters

    letter_list = list(letters)
    for x in lettersGuessed:
        if x in letter_list:
            letter_list.remove(x)
    return ' '.join(letter_list)

print(getAvailableLetters(['a', 'b', 'x', 'z']))
</code></pre>

<p>comprehension:</p>

<pre><code>def getAvailableLetters(lettersGuessed):

    from string import ascii_lowercase as letters

    letter_list = list(letters)
    return ''.join(x for x in lettersGuesseed for x in letter_list if letter_list.remove(x))

print(getAvailableLetters(['a', 'b', 'x', 'z']))
</code></pre>

<p>now when i try the comprehension it's (my IDE, pycharm) saying that the second x is 'local variable not used' and that lettersGuessed is an 'unresolved reference'.</p>

<p>is this one of the times that i can't use comprehension?  and if so, how can i tell without having to try to write it out?</p>
