Home > database >  Python: specify list type at creation without usage of np.float_()
Python: specify list type at creation without usage of np.float_()

Time:08-12

I am working with many lists in which float values will be stored. Is there a simple way to specify the type of a list when creating it? Since I continuously add values to the list in a loop, I would like to avoid the application of np.float_(). Can I specify that all variables that are added are automatically converted to the float type?

Thanks in advance!

CodePudding user response:

Lists in standard out-of-the-box Python are not limited to a particular data type, so it is not foreseen to define one when creating the list. Numpy arrays, on the other hand, have that limitation, so I think your best bet would be to create one via np.array() and specify the data type float with the dtype parameter, e.g.:

arr = np.array([7, 8, 9], dtype=float)
  • Related