how can I delete the keys in a dictionary that has values nan ? with and without creating new dictionary ?
example:
my_dictionary = dict(
first=float("nan"),
second="second"
)
expecting:
{'second': 'second'}
{k:v for k,v in my_dictionary.items() if not isnan(v)}
does not work because isnan(v) takes only number as parameter
>Solution :
Perhaps you can use a dict comprehension using the fact that NaN is not equal to NaN:
out = {k:v for k,v in my_dictionary.items() if v==v}
or using math.isnan:
out = {k:v for k,v in my_dictionary.items() if not isinstance(v, float) or not math.isnan(v)}
Output:
{'second': 'second'}