False iteration over list
<p>I have a function that's suppose to minimize the quantity of a letter in a list if a given word contains that letter. That's the def of the function:</p>

<pre><code>word = 'better'
hand = {'b':1, 'r':1, 's':3, 't':2, 'z':1, 'e':3}
def updateHand(hand, word):
    handCopy = hand.copy()
    for x in word:
        print(x)
        print(hand)
        handCopy[x] = hand.get(x,0) - 1
        print(handCopy)
    return handCopy
</code></pre>

<p>And that's the Output:</p>

<pre><code>b
{'s': 3, 'b': 1, 'z': 1, 't': 2, 'r': 1, 'e': 3}
{'s': 3, 'b': 0, 'z': 1, 't': 2, 'r': 1, 'e': 3}
e
{'s': 3, 'b': 1, 'z': 1, 't': 2, 'r': 1, 'e': 3}
{'s': 3, 'b': 0, 'z': 1, 't': 2, 'r': 1, 'e': 2}
t
{'s': 3, 'b': 1, 'z': 1, 't': 2, 'r': 1, 'e': 3}
{'s': 3, 'b': 0, 'z': 1, 't': 1, 'r': 1, 'e': 2}
t
{'s': 3, 'b': 1, 'z': 1, 't': 2, 'r': 1, 'e': 3}
{'s': 3, 'b': 0, 'z': 1, 't': 1, 'r': 1, 'e': 2}
e
{'s': 3, 'b': 1, 'z': 1, 't': 2, 'r': 1, 'e': 3}
{'s': 3, 'b': 0, 'z': 1, 't': 1, 'r': 1, 'e': 2}
r
{'s': 3, 'b': 1, 'z': 1, 't': 2, 'r': 1, 'e': 3}
{'s': 3, 'b': 0, 'z': 1, 't': 1, 'r': 0, 'e': 2}
Out[110]: {'b': 0, 'e': 2, 'r': 0, 's': 3, 't': 1, 'z': 1}
</code></pre>

<p>Why is my function skipping the second <code>t</code> and / or doesn't eliminate it from the list? Thanks!</p>

<p>Let's consider this situation:</p>

<pre><code>hand = {'b':1, 'r':1, 's':3, 't':2, 'z':1, 'e':3}
</code></pre>

<p>so we have hand as a dict. Now we do this one:</p>

<pre><code>x = hand.get('t',0) - 1
print x
</code></pre>

<p>result will be 1. Let's do it again:</p>

<pre><code>x = hand.get('t',0) - 1
print x
</code></pre>

<p>again 1. Why? Because you're not updating value for <code>'t'</code> key in hand dict. So it's the same situation as in your code:</p>

<pre><code>handCopy[x] = hand.get(x,0) - 1
</code></pre>

<p>so you should do it in this way:</p>

<pre><code>handCopy[x] = handCopy.get(x, 0) - 1
</code></pre>

<p><strong>Solution</strong></p>

<pre><code>word = 'better'
hand = {'b':1, 'r':1, 's':3, 't':2, 'z':1, 'e':3}
def updateHand(hand, word):
    handCopy = hand.copy()
    for x in word:         
        print(x)           
        print(hand)        
        handCopy[x] = handCopy.get(x,0) - 1
        print(handCopy)    
    return handCopy
</code></pre>

<p>result:</p>

<pre><code>b
{'b': 1, 'e': 3, 's': 3, 'r': 1, 't': 2, 'z': 1}
{'s': 3, 'b': 0, 'e': 3, 't': 2, 'r': 1, 'z': 1}
e
{'b': 1, 'e': 3, 's': 3, 'r': 1, 't': 2, 'z': 1}
{'s': 3, 'b': 0, 'e': 2, 't': 2, 'r': 1, 'z': 1}
t
{'b': 1, 'e': 3, 's': 3, 'r': 1, 't': 2, 'z': 1}
{'s': 3, 'b': 0, 'e': 2, 't': 1, 'r': 1, 'z': 1}
t
{'b': 1, 'e': 3, 's': 3, 'r': 1, 't': 2, 'z': 1}
{'s': 3, 'b': 0, 'e': 2, 't': 0, 'r': 1, 'z': 1}
e
{'b': 1, 'e': 3, 's': 3, 'r': 1, 't': 2, 'z': 1}
{'s': 3, 'b': 0, 'e': 1, 't': 0, 'r': 1, 'z': 1}
r
{'b': 1, 'e': 3, 's': 3, 'r': 1, 't': 2, 'z': 1}
{'s': 3, 'b': 0, 'e': 1, 't': 0, 'r': 0, 'z': 1}
Out[110]: {'s': 3, 'b': 0, 'e': 1, 't': 0, 'r': 0, 'z': 1}
</code></pre>

python list dictionary iteration