I HAVE to use pop() method in Python3 to delete all states that starts with 'M'. Can't figure out how to keep the size of the dictionary.
states = {
'AK': 'Alaska',
'AL': 'Alabama',
'AR': 'Arkansas',
'MN': 'Minnesota',
'MO': 'Missouri',
'MP': 'Northern Mariana Islands',
'MS': 'Mississippi',
}
for key, val in states.items():
if key[0] == 'M':
states.pop(key, val)
RuntimeError Traceback (most recent call last)
Input In [45], in <cell line: 62>()
60 # your code here
61 newdic = {}
---> 62 for key, val in states.items():
63 if key[0] == 'M':
64 states.pop(key,val)
RuntimeError: dictionary changed size during iteration