title: How to change a dictionary while iterating over its keys while iterating over a list
body: <ol>
  <li>I have a list l.  </li>
  <li>I have a dictionary d.  </li>
  </ol>
  
  <p>I want to iterate over l. For any list-item, I want to iterate over the d.keys.</p>
  
  <p>If some condition is met, I would like to 'update' my dictionary.  </p>
  
  <p>I have naively tried to nest two for-loops and put in an if-statement -- One cannot change the length of the object one is iterating over.  </p>
  
  <pre><code>d = {'this': '1', 'is': '2', 'a': '3', 'list': '4'}
  
  l = ['A', 'B', 'C', 'D', 'E']
  
  for word in l:
      for key in d.keys():
          if len(key) &lt; 2:#some condition
              d.pop(key)
          else:
              print(word, key)
  </code></pre>
  
  <p>This is the output I get:</p>
  
  <pre><code>A this
  A is
  Traceback (most recent call last):
    File "untitled3.py", line 6, in &lt;module&gt;
      for key in d.keys():
  RuntimeError: dictionary changed size during iteration
  </code></pre>