Home > Back-end >  Why can we pass a list for the shape of a NumPy array? Doesn't the shape have to be an int or a
Why can we pass a list for the shape of a NumPy array? Doesn't the shape have to be an int or a

Time:05-26

I was reading the official NumPy Documentation for the array creation function np.empty(). The page states that empty() takes a shape parameter, allowing the user to specify the shape of the array to be created. The description of the parameter is:

shape: int or tuple of int.
Shape of the empty array, e.g., (2, 3) or 2.

shape is explicitly stated to be an int or a tuple of int(s). Yet, at the bottom of the page is this example, where shape seems to be a list of ints:

>>> np.empty([2, 2])  # [2, 2] uses square brackets, so this is a list!
array([[ -9.74499359e 001,   6.69583040e-309],  # array shape matches the list
       [  2.13182611e-314,   3.06959433e-309]]) # values are uninitialized

Running the example on my machine gave the same results; NumPy seems to allow using a list to specify the shape of an array. So, my questions are

  • Is this intended behavior (allowing a list to be used as the shape of a NumPy array)?
  • Is something else going on? For instance, is the list just being converted to a tuple?

CodePudding user response:

AFAIK, tuples was the only type supported initially for Numpy shapes, but lists was more recently supported due to this related bug. Thus, Numpy developers are aware of this and explicitly choose to support any type of integer-like sequence. In fact, comments in the Numpy code explicitly states:

it can be an arbitrary sequence of integer like objects, neither of which is safe

Thus, lists are supported in the code but it is undocumented yet (the documentation is not up to date). Fun fact: range(2, 6) is a completely valid shape regarding the Numpy code.

Internally, all shapes descriptors are tuples (see the code). Thus, yes, lists and more generally integer-like sequences (including the special case of a scalar integer) are converted to a tuple.

CodePudding user response:

You can use tuples and list interchangeably in NumPy in most cases, as such that:

a = np.array([1,2,3,4,5,6,7,8,9])
print(a) # [1 2 3 4 5 6 7 8 9]

b = np.array((1,2,3,4,5,6,7,8,9))
print(b) # [1 2 3 4 5 6 7 8 9]

print(np.array_equal(a, b))  # True
  • Related