Python 3.5 dictionary comparison
<p>I am trying to compare all elements of one dictionary to make sure they are in a second with the correct number. I am new at Python so I know there is something simple I am probably missing and I have been working on this one problem for hours so my code is likely very ugly and wrong. Here is an example of what I have so far.</p>

<pre><code>try:       
    for key in dict_one:
        if dict_two.get(key, 0) == dict_one[key]:
           del dict_one[key]
           if dict_one[key] &lt; 0 :                
              return False
        else:
             return True
except KeyError:
   pass
</code></pre>

<p>I have tried <code>all(dict_two.get(key,0))</code> as well and it didn't work.  The final output should check that you can spell a word from <code>dict_two</code> using the words in <code>dict_one</code> <code>True</code> if you can, <code>False</code> if you can't so if <code>dict_two</code> word requires three Es then <code>dict_one</code> should have 3 Es or return false. Or two Ns if you were spelling bunny ex <code>dict_one = {b: 1, u: 1, n:1, y:1, x: 3}</code> and <code>dict_two ={b: 1, u: 1. n: 2, y:1}</code> <code>False</code> because you need 2 Ns in the word and <code>dict_one</code> only has one. </p>

<p>I can get <code>dict_two</code> to populate correctly when I enter a word and <code>dict_one</code> properly pulls random numbers and amounts of those numbers. And I can get them to compare properly for letters included in each, I just can't get it to produce right answer of <code>True</code> or <code>False</code> for the number of letters needed. I feel I am close to an answer, but then just make it worse when I try new things and dig my hole deeper. </p>

<p>Thank you! </p>

<p>So you want to check to see that every letter in <code>dict2</code> has at mapping in <code>dict1</code> least as large as that letters mapping in <code>dict2</code>?  That's accomplished fairly easily.</p>

<pre><code>def can_spell(dict1, dict2):
    try:
        return all(dict1[k] &gt;= v for k, v in dict2.items())
    except KeyError:
        return False
</code></pre>

<p>This gets every (key, value) pair in <code>dict2</code> and then compares <code>v</code> with the mapping of that key in <code>dict1</code>.  <code>all</code> returns <code>True</code> iff every expression in that generator comprehension is true.</p>

python python-3.x dictionary