Question: I have an array of complex *dict* that have some value as a string "NULL" and I want to remove, my dict looks like this:

```
d = [{
  "key1": "value1",
  "key2": {
    "key3": "value3",
    "key4": "NULL",
    "z": {
       "z1": "NULL",
       "z2": "zzz",
    },
  },
  "key5": "NULL"
}, {
  "KEY": "NULL",
  "AAA": "BBB",
}]

```

And I want to wipe out all keys that have **NULL** as value

Like this:

`[{"key1": "value1", "key2": {"key3": "value3", "z": {"z2": "zzz"}}}, {"AAA": "BBB"}]`

I am using Python 3.9, so it is possible to use walrus operator.

Answer: 
**Solution:**

```
d = [{
  "key1": "value1",
  "key2": {
    "key3": "value3",
    "key4": "NULL",
    "z": {
       "z1": "NULL",
       "z2": "zzz",
    },
  },
  "key5": "NULL"
}, {
  "KEY": "NULL",
  "AAA": "BBB",
}]

for element in list(d):
    for key, value in element.copy().items():
        if value == "NULL":
            element.pop(key, None)
        elif isinstance(value, dict):
            for inner_key, inner_value in value.copy().items():
                if inner_value == "NULL":
                    value.pop(inner_key, None)
                elif isinstance(inner_value, dict):
                    for nested_inner_key, nested_inner_value in inner_value.copy().items():
                        if nested_inner_value == "NULL":
                            inner_value.pop(nested_inner_key, None)
print(d)

```

**Output:**

```
[{'key1': 'value1', 'key2': {'key3': 'value3', 'z': {'z2': 'zzz'}}}, {'AAA': 'BBB'}]

```

Doing `.copy()` of each dictionary / nested dictionary, else you'll end up with [this](https://stackoverflow.com/questions/10107676/python-runtimeerror-dictionary-changed-size-during-iteration) error.

Check out the same [here](https://onlinegdb.com/wYDUvPhFQ) also.
> 
> I am using Python 3.9, so it is possible to use walrus operator.
> 
> 
> 

Well then, let's throw one in there since no one else did:

```
from collections import deque

def remove_null(thing):
    if isinstance(thing, list):
        deque(map(remove_null, thing), maxlen=0)
    elif isinstance(thing, dict):
        for key in thing.copy():
            if (value := thing[key]) == 'NULL':
                del thing[key]
            else:
                remove_null(value)

```

Note that I'm using `map` instead of a loop to show an alternative to the other answers. However, this creates the same situation that I commented on @AnnZen about, using an operation that returns a result and then ignoring it. To force the `map` lazy evaluation, I use an empty `deque` to consume it.