I have a dictionary:
{'key1':1, 'key2':2, 'key3':3}
I need to pass a sub-set of that dictionary to third-party code. It only wants a dictionary containing keys ['key1', 'key2', 'key99']
and if it gets another key (eg 'key3'
), it explodes in a nasty mess. The code in question is out of my control so I'm left in a position where I have to clean my dictionary.
What's the best, way to limit a dictionary to a set of keys?
Given the example dictionary and allowed keys above, I want:
{'key1':1, 'key2':2}