I have these keys:
keep = ["a","c"]
My dict:
testdict = {'
'a':'vala',
'b':'valb',
'c':'valc',
'd':'vald'
}
Desired output:
testdict = {
'a':'vala',
'c':'valc'
}
I want to remove all keys that do not match the key in the list. What is the fastest way to do this?
I tried:
for key, value in testdict.iteritems():
if key not in keep:
del testdict[key]
But the above gives me errors since the size is changing.