title: Deleting items from a dictionary with a for loop
body: <p>I'm trying to drop items from a dictionary if the value of the key is below a certain threshold. For a simple example to what I mean:</p>
  
  <pre><code>my_dict = {'blue': 1, 'red': 2, 'yellow': 3, 'green': 4}
  
  for color in my_dict:
      threshold_value = 3
      if my_dict[color] &lt; threshold_value:
          del my_dict[color]
  
  print(my_dict)
  </code></pre>
  
  <p>Now, I get a <code>RuntimeError: dictionary changed size during iteration</code> error. No big surprises there. The reason I'm posting this question is:</p>
  
  <ol>
  <li><p>Find out if there's an elegant solution that doesn't require creating a new dictionary (that holds only the keys with values >= threshold).</p></li>
  <li><p>Try to understand Python's rationale here. The way I read it to myself is: "go to the first key. Is the value of that key &lt; x ? if yes - del this key:value item and continue on the the next key in the dictionary, if no - continue to next key without doing anything". In other words, what happened historically to previous keys shouldn't affect where I go next. I'm looking forward to the next items, regardless of the past.
  I know it's a funny (some might say stupid, I'll give you that) but what's Python's "way of thinking" about this loop? Why doesn't it work? How would Python read it out loud to itself? Just trying to get a better understanding of the language...</p></li>
  </ol>