The shape attribute for numpy arrays returns the dimensions of the array. If Y has n rows and m columns, then Y.shape is (n,m). So Y.shape[0] is n.

In [46]: Y = np.arange(12).reshape(3,4)

In [47]: Y
Out[47]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

In [48]: Y.shape
Out[48]: (3, 4)

In [49]: Y.shape[0]
Out[49]: 3
Answer from unutbu on Stack Overflow
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.shape.html
numpy.shape — NumPy v2.5.dev0 Manual
Equivalent array method. ... Try it in your browser! >>> import numpy as np >>> np.shape(np.eye(3)) (3, 3) >>> np.shape([[1, 3]]) (1, 2) >>> np.shape([0]) (1,) >>> np.shape(0) ()
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_shape.asp
NumPy Array Shape
NumPy Editor NumPy Quiz NumPy Exercises NumPy Syllabus NumPy Study Plan NumPy Certificate ... The shape of an array is the number of elements in each dimension.
Discussions

python - What does .shape[] do in "for i in range(Y.shape[0])"? - Stack Overflow
Y is a matrix of data but I can't ... .shape[0] does exactly. ... This program uses numpy, scipy, matplotlib.pyplot, and cvxopt. ... docs.scipy.org/doc/numpy/reference/generated/… or docs.scipy.org/doc/numpy/reference/generated/… ... The shape attribute for numpy arrays returns the ... More on stackoverflow.com
🌐 stackoverflow.com
Is there a difference between array.shape[0] and len(array) for a numpy array?
Don’t think so — I’d probably use arr.shape[0] regardless since it’s a bit more explicit what it’s doing (size of the array’s first dimension). More on reddit.com
🌐 r/learnpython
3
1
September 26, 2020
python - x.shape[0] vs x[0].shape in NumPy - Stack Overflow
x is a 2D array, which can also be looked upon as an array of 1D arrays, having 10 rows and 1024 columns. x[0] is the first 1D sub-array which has 1024 elements (there are 10 such 1D sub-arrays in x), and x[0].shape gives the shape of that sub-array, which happens to be a 1-tuple - (1024, ). More on stackoverflow.com
🌐 stackoverflow.com
Difference between .shape[0] and .shape[1]
Hi, In the course, i find sometimes the code is written as m=X.shape[0] and n=w.shape[1]. Can you tell me the difference between these 2 functions, .shape[0] and .shape[1], though both returns the number of columns in an array More on community.deeplearning.ai
🌐 community.deeplearning.ai
0
0
August 27, 2022
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.shape.html
numpy.shape — NumPy v2.3 Manual
Equivalent array method. ... Try it in your browser! >>> import numpy as np >>> np.shape(np.eye(3)) (3, 3) >>> np.shape([[1, 3]]) (1, 2) >>> np.shape([0]) (1,) >>> np.shape(0) ()
🌐
Codegive
codegive.com › blog › numpy_array_shape_0.php
Numpy array shape 0
scalar_arr = np.array(42) print(f"Scalar array: {scalar_arr}") print(f"Shape: {scalar_arr.shape}") # Output: () print(f"Size: {scalar_arr.size}") # Output: 1 print(f"ndim: {scalar_arr.ndim}") # Output: 0 · print(f"Value: {scalar_arr.item()}") # Output: 42 print(f"Value: {scalar_arr[()]}") # Output: 42 (special indexing for scalars) print(f"Scalar + 5: {scalar_arr + 5}") # Output: 47 print(f"Scalar.sum(): {scalar_arr.sum()}") # Output: 42 While important, these are distinct from arrays that are "empty" due to a dimension having a length of zero. NumPy arrays with zero-length dimensions (e.g., shape=(0,), (0, 5), (3, 0)) are a powerful and integral part of NumPy's design for handling datasets where the number of elements in a given dimension can be zero.
Top answer
1 of 4
22

x is a 2D array, which can also be looked upon as an array of 1D arrays, having 10 rows and 1024 columns. x[0] is the first 1D sub-array which has 1024 elements (there are 10 such 1D sub-arrays in x), and x[0].shape gives the shape of that sub-array, which happens to be a 1-tuple - (1024, ).

On the other hand, x.shape is a 2-tuple which represents the shape of x, which in this case is (10, 1024). x.shape[0] gives the first element in that tuple, which is 10.

Here's a demo with some smaller numbers, which should hopefully be easier to understand.

x = np.arange(36).reshape(-1, 9)
x

array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8],
       [ 9, 10, 11, 12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23, 24, 25, 26],
       [27, 28, 29, 30, 31, 32, 33, 34, 35]])

x[0]
array([0, 1, 2, 3, 4, 5, 6, 7, 8])

x[0].shape
(9,)

x.shape
(4, 9)

x.shape[0]
4
2 of 4
10

x[0].shape will give the Length of 1st row of an array. x.shape[0] will give the number of rows in an array. In your case it will give output 10. If you will type x.shape[1], it will print out the number of columns i.e 1024. If you would type x.shape[2], it will give an error, since we are working on a 2-d array and we are out of index. Let me explain you all the uses of 'shape' with a simple example by taking a 2-d array of zeros of dimension 3x4.

import numpy as np
#This will create a 2-d array of zeroes of dimensions 3x4
x = np.zeros((3,4))
print(x)
[[ 0.  0.  0.  0.]
[ 0.  0.  0.  0.]
[ 0.  0.  0.  0.]]

#This will print the First Row of the 2-d array
x[0]
array([ 0.,  0.,  0.,  0.])

#This will Give the Length of 1st row
x[0].shape
(4,)

#This will Give the Length of 2nd row, verified that length of row is showing same 
x[1].shape
(4,)

#This will give the dimension of 2-d Array 
x.shape
(3, 4)

# This will give the number of rows is 2-d array 
x.shape[0]
3

# This will give the number of columns is 2-d array 
x.shape[1]
3

# This will give the number of columns is 2-d array 
x.shape[1]
4

# This will give an error as we have a 2-d array and we are asking value for an index 
out of range
x.shape[2]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-20-4b202d084bc7> in <module>()
----> 1 x.shape[2]

IndexError: tuple index out of range
Find elsewhere
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.shape.html
numpy.shape — NumPy v2.4 Manual
Equivalent array method. ... Try it in your browser! >>> import numpy as np >>> np.shape(np.eye(3)) (3, 3) >>> np.shape([[1, 3]]) (1, 2) >>> np.shape([0]) (1,) >>> np.shape(0) ()
🌐
DeepLearning.AI
community.deeplearning.ai › course q&a › machine learning specialization › advanced learning algorithms
Difference between .shape[0] and .shape[1] - Advanced Learning Algorithms - DeepLearning.AI
August 27, 2022 - Hi, In the course, i find sometimes the code is written as m=X.shape[0] and n=w.shape[1]. Can you tell me the difference between these 2 functions, .shape[0] and .shape[1], though both returns the number of columns in a…
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.zeros.html
numpy.zeros — NumPy v2.1 Manual
Return a new array of given shape filled with value. ... >>> np.zeros((2,), dtype=[('x', 'i4'), ('y', 'i4')]) # custom dtype array([(0, 0), (0, 0)], dtype=[('x', '<i4'), ('y', '<i4')])
🌐
Medium
medium.com › @amit25173 › understanding-numpy-shape-6fbb6b83891e
Understanding numpy.shape. If you think you need to spend $2,000… | by Amit Yadav | Medium
February 9, 2025 - For a 1D array, the shape might look like (n,), where n is the number of elements. For a 3D array, it could look like (x, y, z), representing depth, rows, and columns. Here’s another quick example to make it more fun. You might wonder: “What if I have a 3D array?” Let’s explore that: # Creating a 3D NumPy array array_3d = np.array([ [[1, 2], [3, 4]], [[5, 6], [7, 8]] ]) print("3D Array Shape:", array_3d.shape)
🌐
Python Guides
pythonguides.com › python-numpy-shape
NumPy Shape And Array Dimensions In Python
May 16, 2025 - The .ndim property is a simple way to inspect the structure of any NumPy array. Let me explain to you some practical examples that will help you to understand more about the NumPy shape. ... import numpy as np def process_monthly_data(data): # Check if the data has 12 months if data.shape[0] != 12: raise ValueError(f"Expected 12 months of data, got {data.shape[0]}") # Process data here return data.mean() # Test with correct data monthly_sales = np.random.randint(1000, 5000, size=12) print("Average monthly sales:", process_monthly_data(monthly_sales)) # Test with incorrect data try: quarterly_sales = np.random.randint(1000, 5000, size=4) process_monthly_data(quarterly_sales) except ValueError as e: print("Error:", e)
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.shape.html
numpy.shape — NumPy v2.1 Manual
Equivalent array method. ... >>> import numpy as np >>> np.shape(np.eye(3)) (3, 3) >>> np.shape([[1, 3]]) (1, 2) >>> np.shape([0]) (1,) >>> np.shape(0) ()
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.shape.html
numpy.shape — NumPy v2.2 Manual
Equivalent array method. ... >>> import numpy as np >>> np.shape(np.eye(3)) (3, 3) >>> np.shape([[1, 3]]) (1, 2) >>> np.shape([0]) (1,) >>> np.shape(0) ()
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.ndarray.shape.html
numpy.ndarray.shape — NumPy v2.4 Manual
>>> import numpy as np >>> x = np.array([1, 2, 3, 4]) >>> x.shape (4,) >>> y = np.zeros((2, 3, 4)) >>> y.shape (2, 3, 4) >>> y.shape = (3, 8) >>> y array([[ 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0.]]) >>> y.shape = (3, 6) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: cannot reshape array of size 24 into shape (3,6) >>> np.zeros((4,2))[::2].shape = (-1,) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: Incompatible shape for in-place modification.
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Get the dimensions, shape, and size of an array | note.nkmk.me
April 23, 2025 - You can get the size, i.e., the total number of elements, of a NumPy array with the size attribute. ... print(a_1d.size) # 3 print(type(a_1d.size)) # <class 'int'> print(a_2d.size) # 12 print(a_3d.size) # 24 ... len() is a built-in Python function that returns the number of elements in a list or the number of characters in a string. ... For a numpy.ndarray, len() returns the size of the first dimension, which is equivalent to shape[0]. It is also equal to size only for one-dimensional arrays.
🌐
DigitalOcean
digitalocean.com › community › tutorials › numpy-zeros-in-python
numpy.zeros() in Python | DigitalOcean
August 3, 2022 - Python numpy.zeros() function returns a new array of given shape and type, where the element’s value as 0.
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-array-shape
NumPy Array Shape - GeeksforGeeks
July 15, 2025 - The shape of an array can be defined as the number of elements in each dimension. Dimension is the number of indices or subscripts, that we require in order to specify an individual element of an array. In NumPy, we will use an attribute called shape which returns a tuple, the elements of the tuple give the lengths of the corresponding array dimensions.