[Numpy-discussion] array - dimension size of 1-D and 2-D examples

Derek Homeier derek at astro.physik.uni-goettingen.de
Sat Dec 30 18:11:48 EST 2017

On 30 Dec 2017, at 5:38 pm, Vinodhini Balusamy <me.vinob at gmail.com> wrote:
> Just one more question from the details you have provided which from my understanding strongly seems to be Design  
> [DEREK] You cannot create a regular 2-dimensional integer array from one row of length 3
>> and a second one of length 0. Thus np.array chooses the next most basic type of
>> array it can fit your input data in
Indeed, the general philosophy is to preserve the structure and type of your input data
as far as possible, i.e. a list is turned into a 1d-array, a list of lists (or tuples etc…) into
a 2d-array,_ if_ the sequences are of equal length (even if length 1).
As long as there is an unambiguous way to convert the data into an array (see below).

>    Which is the case,  only if an second one of length 0 is given.
>    What about the case 1 :
> >>> x12 = np.array([[1,2,3]])
> >>> x12
> array([[1, 2, 3]])
> >>> print(x12)
> [[1 2 3]]
> >>> x12.ndim
> 2
> >>>
> >>>
> This seems to take 2 dimension.

Yes, structurally this is equivalent to your second example

> also,
>>> x12 = np.array([[1,2,3],[0,0,0]])
>>> print(x12)
[[1 2 3]
 [0 0 0]]
>>> x12.ndim

> I presumed the above case and the case where length 0 is provided to be treated same(I mean same behaviour).
> Correct me if I am wrong.
In this case there is no unambiguous way to construct the array - you would need a shape (2, 3)
array to store the two lists with 3 elements in the first list. Obviously x12[0] would be np.array([1,2,3]),
but what should be the value of x12[1], if the second list is empty - it could be zeros, or repeating x12[0],
or simply undefined. np.array([1, 2, 3], [4]]) would be even less clearly defined.
These cases where there is no obvious “right” way to create the array have usually been discussed at
some length, but I don’t know if this is fully documented in some place. For the essentials, see


note also the upcasting rules if you have e.g. a mix of integers and reals or complex numbers,
and also how to control shape or data type explicitly with the respective keywords.


More information about the NumPy-Discussion mailing list