title: Iterating a dictionary while modifying it concurrently
body: <p>I have a piece of code where I would like to iterate through a Python dictionary while it is being modified from a separate thread. Specifically, the other thread may: 1) Add new keys, 2) Modify the values in existing keys. It will <em>not</em> delete any keys.</p>
  <p>This code should also be as performant as possible. It is okay if we miss the newer keys when iterating.</p>
  <p>My code currently looks like this:</p>
  <pre><code>### THREAD 1
  def add_data(my_dict, key, state):
    if key not in my_dict:
      # Adding of new keys is infrequent, but frequent enough that it causes errors sometimes.
      my_dict[key] = SomeClass()
    my_dict[key].change_state(state)
  
  ### THREAD 2
  def get_data(my_dict):
    return [v.transform_in_some_way() for k, v in my_dict.items()]
  </code></pre>
  <p>This code occasionally throws <code>RuntimeError: dictionary changed size during iteration</code>.</p>
  <p>I have since changed the code in Thread 2 to be like this:</p>
  <pre><code>### THREAD 2
  def get_data(my_dict):
    return [my_dict[k].transform_in_some_way() for k in list(my_dict.keys())]
  </code></pre>
  <p>In simple tests, this code has worked well, and I no longer see the <code>RuntimeError</code> - but I am not sure if I've truly fixed the problem, or if I've only made it less frequent. Which is it?</p>
  <hr />
  <p>If this is not an appropriate fix - is there any way to iterate a Python dictionary while adding keys to it concurrently, and avoid extra synchronization? is there any thread-safe way to get a list of dictionary keys?</p>