0

I know that I can create a subset of a dictionary by the following code:

#I get only fields a,b and c from the old dict
newdict = {k: olddict.get(k, None) for k in ('a', 'b', 'c')}

but suppose I need all fields, except "d" and "e"..is there a way to reverse this code?

Phate
  • 6,066
  • 15
  • 73
  • 138
  • More [here](https://stackoverflow.com/questions/43618542/exclude-specific-keys-of-dict-when-passing-to-variable) – Sheldore Jun 10 '19 at 16:12

1 Answers1

5

Just inverse it with not keyword:

newdict = {k: olddict.get(k, None) for k in olddict if k not in ('d', 'e')}

vurmux
  • 9,420
  • 3
  • 25
  • 45