0

I would like to delete all the keys from a JSON file, that contain the words "spotify" and "album". When I execute the following code, it works:

for key in list(data.keys()):
    if "spotify" in key:
        del data[key]

When I add a second word, it returns an empty dict:

for key in list(data.keys()):
    if "spotify" or "album" in key:
        del data[key]

What am I doing wrong?

mkrieger1
  • 14,486
  • 4
  • 43
  • 54
Cait
  • 39
  • 5
  • You could use [`dict.pop`](https://docs.python.org/3/library/stdtypes.html#dict.pop) with default argument. Something like `for k in ["spotify", "album"]: data.pop(k, None)` or using dict comp `data = {k: data[k] for k in data.keys() - ["spotify", "album"]}` – Ch3steR Nov 12 '21 at 13:18

0 Answers0