numpy.shape() – The NumPy Shape Python Function

Introduction to numpy.shape() Function

Guide to numpy.shape() Function – The number of elements in each dimension determines the form of an array. The number of indices or subscripts required to specify an individual member of an array is known as dimension.

In NumPy, we’ll utilize the shape property. Which produces a tuple with the lengths of the respective array dimensions as its members.



Parameters: The array is pass as a Parameter.

Return: A tuple whose elements give the lengths of the corresponding array dimensions.

Code Examples of numpy.shape() Function

Example 01: Printing the shape of the multidimensional array

# Welcome to
import numpy as npy
# creating a 2-d array
arr1 = npy.array([[4, 5, 6, 7], [1, 2, 3, 4]])
# creating a 3-d array
arr2 = npy.array([[[8, 7], [6, 5]], [[4, 3], [2, 1]]])
# printing the shape of arrays
# first element of tuple gives
# dimension of arrays second
# element of tuple gives number
# of element of each dimension


(2, 4)
(2, 2, 2)


  • Line1 – Imports the NumPy Python Library
  • Line2 – Creates a 2d Array
  • Line3 – Creates a 3d Array
  • Line4 – Print array 1
  • Line5 – Print array 2

The above example gives (2, 4) and (2,2,2), indicating that the arr1 contains two dimensions, each with four elements. Similarly, arr2 has three dimensions, each with two rows and two columns.

Example 02: Creating an array using ndmin using a vector with values 6, 3, 5, 6, 12, 2 and verifying the value of the last dimension

# Welcome to
import numpy as npy
# creating an array of 6 dimension
# using ndim
arr = npy.array([6, 3, 5, 6, 12, 2], ndmin=7)
# printing array
# verifying the value of last dimension
print('shape of an array :', arr.shape)


[[[[[[[ 6  3  5  6 12  2]]]]]]]
shape of an array : (1, 1, 1, 1, 1, 1, 6)

In the above example, we verified the last value of dimension as 6.


What is shape in Python NumPy?

The shape property is often use to obtain an array’s current form. But it may also be use to reshape it in place by passing it a tuple of array dimensions. The same goes for NumPy.

How do I index a NumPy array?

In Numpy, indexing is done by utilizing an array as an index. A view or shallow copy of the array is return in the case of a slice. But a copy of the original array is return in the case of the index array. With the exception of tuples. Numpy arrays can be index with other arrays or any other sequence.


That’s all for this article if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn.

Suggested Articles:

  1. numpy.copyto() – The NumPy copyto Python Function

Leave a Comment