NumPy shape()

The shape() method returns the shape of an array i.e. the number of elements in each dimension.


shape() Syntax

The syntax of shape() is:

numpy.shape(array)

shape() Argument

The shape() method takes a single argument:

  • array - an array whose shape is to be determined

shape() Return Value

The shape() method returns the shape of an array as a tuple.


Example 1: Shape of Arrays

Output

Shape of the first array :  (4,)
Shape of the second array :  (1, 2)

Example 2: Shape of Array of Tuples

Output

Shape of first array :  (2, 2)
Shape of second array :  (2, )

Here, array1 and array2 are 2-dimensional arrays with tuples as their elements. The shape of array1 is (2, 2). However, the shape of array2 is (2, ), which is one dimensional.

This is because we've passed the dtype argument, which restricts the structure of array2.

array2 contains two elements, each of which is a tuple with two integer values, but each element is treated as a single entity with two fields x and y.

Therefore, array2 is one dimensional with two rows and one column.