Q:

RuntimeError: dictionary changed size during iteration - how to solve?

I am trying to traverse through a dictionary and capitalize all of the keys if they are strings.  I am getting a RunTime error for this:
for k, v in ref_dict.items():
    if isinstance(k, str):
        ref_dict[k.upper()] = v
    else:
        ref_dict[k] = v

How do I avoid something like this? Or, better, how do I improve the code in order to solve this more elegantly?

A:

How big is your dict? If it is not huge, then you could do:
new_dict = {}
for k, v in ref_dict.items():
    if isinstance(k, str):
        new_dict[k.upper()] = v
    else:
        new_dict[k] = v

ref_dict = new_dict

In the other case, you may need to review your creation of ref_dict and modify the key there.
Also, one could modify your original code as follows change ref_dict. But it's worth mentioning that it would add new elements to ref_dict. For example
ref_dict = {'abc' : 1}

### change of code
keys = [k for key in ref_dict]

for k in keys:
    if isinstance(k, str):
        ref_dict[k.upper()] = ref_dict[k]
    # else part is not neccessary
    # else:
    #     ref_dict[k] = v

# now ref_dict = {'abc':1, 'ABC':1}

