[Numpy-discussion] array not appending

Alan G Isaac aisaac@american....
Thu Dec 18 08:05:03 CST 2008


On 12/18/2008 5:56 AM Prashant Saxena apparently wrote:
> ST = np.empty((), dtype=np.float32)
> ST = np.append(ST, 10.0)

If you really need to append elements,
you probably want to use a list and
then convert to an array afterwards.

But if you know your array size,
you can preallocate memory and then fill it.
E.g.,

 >>> ST = np.empty((10,),dtype=np.float32)
 >>> for i in range(10): ST[i]=i

hth,
Alan Isaac

PS Some users suggest it is better practices
to use `zeros` rather than `empty`.  Note that
`zeros` has the same property that surprised you.

 >>> np.zeros((),dtype=np.float32)
array(0.0, dtype=float32)
 >>> np.append(np.zeros((),dtype=np.float32),99)
array([  0.,  99.])



More information about the Numpy-discussion mailing list