Home > OS >  Replace all values in a list with np.nan
Replace all values in a list with np.nan

Time:12-06

How to replace all values within a list with np.nan?

a = [1.58, 2.13, 3.98, 4.12]

and what i want is

a = [nan, nan, nan, nan]

I have tried many options like replace or list comprehension, but it didn't work.

CodePudding user response:

A numpy way of doing it natively:

a = np.full(shape=4, fill_value=np.nan).tolist()

Scales well to higher dimensions and larger size.

CodePudding user response:

import numpy as np
a = [1,2,3,4]
a = [np.nan]*len(a)
print(a)

Output:

[nan, nan, nan, nan]

CodePudding user response:

import numpy as np
a = np.full_like(a, np.nan).tolist()
  • Related