0

I have this array a = ["one", "two"] and I want to match these values in dictionary I have which looks like this

b = {one: "something", two: "something2", three: "something3"}

I want to match this key and remove the keys from the dictionary

In the end it will be something like this

{'three': 'something3'}

what I have tried is this

for t in a:
    b = b.pop(t, None)
    return b

Just want to know if there is a better way to do this

azro
  • 53,056
  • 7
  • 34
  • 70

2 Answers2

0

You could also create a new dict:

c = {key: value for key, value in b.items() if key not in a}
Elias
  • 71
  • 4
-1

Just use del

a = ["one", "two"]
b = {"one": "something", "two": "something2", "three": "something3"}

for key in a:
    del b[key]

print(b)
# {'three': 'something3'}
azro
  • 53,056
  • 7
  • 34
  • 70