I have no problem with any of the existing answers, but I was surprised when I did not find this solution:
keys_to_remove = ['a', 'b', 'c'] my_dict = {k: v for k, v in zip("abcdefg".split(' '), [0, 1, 2, 3, 4, 5, 6])} for k in keys_to_remove: try: del my_dict[k] except KeyError: pass assert my_dict == {'d': 3, 'e': 4, 'f': 5, 'g': 6}
Note. I came across this question coming from here . And my answer is related to this answer .
Doug R. May 20, '15 at 13:37 2015-05-20 13:37
source share