When deleting a key from a dictionary, I use:
if \'key\' in my_dict: del my_dict[\'key\']
Is there a one line way of doing this?
I prefer the immutable version
foo = { 1:1, 2:2, 3:3 } removeKeys = [1,2] def woKeys(dct, keyIter): return { k:v for k,v in dct.items() if k not in keyIter } >>> print(woKeys(foo, removeKeys)) {3: 3} >>> print(foo) {1: 1, 2: 2, 3: 3}