[Numpy-discussion] initializing an array of lists

Robert Kern robert.kern at gmail.com
Mon Nov 9 13:03:06 EST 2009

On Mon, Nov 9, 2009 at 12:00, Christopher Barker <Chris.Barker at noaa.gov> wrote:
> alan at ajackson.org wrote:
>> myarray = zeros( (xdim,ydim), dtype=object)
>> and then iterate through the elements initializing then to empty lists, but
>> surely there is a better way.
> I tried this:
> In [3]: a = np.empty((2,3), dtype=np.object)
> In [5]: a[:,:] = []
> but got:
> ValueError: shape mismatch: objects cannot be broadcast to a single shape
> Is that a bug? Or is it simply too ambiguous for numpy to figure out
> what the heck I want?

The latter.

In any case, you wouldn't want each element to be the same list object.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco

More information about the NumPy-Discussion mailing list