2

Hi I have a list of dictionaries, each dictionary has a list of values. In that list there are nan values which I wish to remove. Here is an example dictionary;

temp = {'A': ['field1', 'field2', 'field3', np.nan, np.nan], 'B': ['field1', 'field2', 'field3', 'field4', np.nan]}

which looks like;

{'A': ['field1', 'field2', 'field3', nan, nan], 'B': ['field1', 'field2', 'field3', 'field4', nan]}

I desired output is :

{'A': ['field1', 'field2', 'field3'], 'B': ['field1', 'field2', 'field3', 'field4']}

I've tired the following with no success;

res = {k:v for k,v in temp2.items() if v is not np.nan}

Any help is appreciated

ukbaz
  • 519
  • 2
  • 14
  • 30

1 Answers1

6

You are comparing the whole value (v in your case) with np.nan, but only single elements of the value of the dictionary are np.nan. You want:

res = {k:[elem for elem in v if elem is not np.nan] for k,v in temp.items()}
mrCarnivore
  • 4,638
  • 2
  • 12
  • 29
  • 1
    You could also use the [`numpy.isnan`](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.isnan.html) method. – saud Nov 17 '17 at 12:55