[Numpy-discussion] Creating array containing empty lists

Stéfan van der Walt stefan@sun.ac...
Wed Mar 25 06:43:03 CDT 2009


2009/3/25 Jesper Larsen <jesper.webmail@gmail.com>:
> import numpy as npy
> a = npy.empty((2), dtype=npy.object_)
>
> # Works fine:
> for i in range(len(a)):
>  a[i] = []
> print a
>
> # Does not work:
> a[:] = []
> a[:] = list()

Slightly simpler would be:

In [26]: x = np.empty((2,), dtype=object)
In [27]: x[:] = [[] * len(x)]

Stéfan


More information about the Numpy-discussion mailing list