You have at least three errors in your thinking.
The first is that an empty list is the same as None
.
Only None
is the same as None
.
Secondly, comparing lists, you should use ==
. Using is
to compare a list means that, even if a list has the same values in it, if it's not the actual same memory address, it won't compare equal.
Also, as you only want to know if the list is empty, you can use the fact that in Python, empty sequences are considered False
and non-empty are considered True
, so you can use a boolean conditional: if not value:
which will be True
for an empty list. If values can be something other than lists, then empty strings, zeroes, etc are all also False
, so you might want to check more carefully.
Thirdly, you shouldn't modify the size of a container like a dict
whilst iterating over it.
Either iterate over a copy of it, or create a record of things you want to modify, and then perform the modification afterwards.
The first way, iterating over a copy:
for key, value in list(d.items()):
if not value:
del d[key]
The second way, making a set of the keys to remove:
keys_to_remove = {key for key, value in d.items()
if not value}
for key in keys_to_remove:
del d[key]