Shape relates to the size of the dimensions of an N-dimensional array.

Size regarding arrays, relates to the amount (or count) of elements that are contained in the array (or sometimes, at the top dimension of the array - when used as length).

For example, let a be a matrix

1  2  3  4
5  6  7  8
9 10 11 12

the shape of a is (3, 4), the size of a is 12 and the size of a[1] is 4.

Answer from Uriel on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › what's the difference between a numpy array with shape (x,) and (x, 1)?
r/learnpython on Reddit: what's the difference between a numpy array with shape (x,) and (x, 1)?
October 6, 2023 -

hey, im doing an ai thing in school and my code didnt work as expected, and after 5 hours i found out i reshaped an array from (206,) to (206,1) and that made the results wrong. and from what i understand, the shape means the length of each dimension, and length is not 0 indexed so a size of 1 would be equal to just 1D no?

Discussions

Difference between numpy shape(), size(), and itemsize() - Week 8: High dimensional array and creating numpy array
please explain the difference in between arr.shape, arr.size and arr.itemsize More on forum.onefourthlabs.com
🌐 forum.onefourthlabs.com
0
0
May 27, 2020
numpy: dimensions vs. shape - Stack Overflow
I have a just a short question about the attributes shape and ndim. How can an array have more dimensions than two, but matrices are limited to a n x m shape? Would it not be necessary to have shapes More on stackoverflow.com
🌐 stackoverflow.com
python - shape vs len for numpy array - Stack Overflow
So again, in your case, I would say if you want information about the whole dataframe just to check or for example to pass the shape tuple to a function, use shape. For a single column, including index (i.e. the rows of a df), use len(). ... The question was about numpy and your answer is about ... More on stackoverflow.com
🌐 stackoverflow.com
What is numpy actually used for?
If you don’t work with anything that involves math then you probably never have to worry about numpy. A large population of python users though work with scientific computing, where it’s math all the way down. I’m talking scalar-scalar operations (1 + 2) scalar-vector operations (2 + np.array([1, 2])) vector-vector operations (np.array([...]) + np.array([...])) vector-matrix (Ax = b) matrix-matrix (AB = C) vectorized math (np.sin(np.array([...])) other things like dot products, cross products, inverses of matrices, root finding and fitting algorithms, polynomial functions, basis functions, etc And so on. These are things I use numpy for every single day. More on reddit.com
🌐 r/learnpython
8
1
June 27, 2018
🌐
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 - 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.
🌐
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.
🌐
Sololearn
sololearn.com › en › Discuss › 2657475 › ds-numpy-size-and-shape
DS, Numpy size and shape | Sololearn: Learn to code for FREE!
Size means the number of element in the array, so here since there are 45 presidents, and their height and age is 45 each, when we concatente them together, we will get 90. Meanwhile for the shape, it represents the number of rows and columns.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.ndarray.shape.html
numpy.ndarray.shape — NumPy v2.2 Manual
The shape property is usually used to get the current shape of an array, but may also be used to reshape the array in-place by assigning a tuple of array dimensions to it. As with numpy.reshape, one of the new shape dimensions can be -1, in which case its value is inferred from the size of the ...
🌐
Delft Stack
delftstack.com › home › howto › numpy › size and shape of array in python
How to Shape and Size of Array in Python | Delft Stack
March 4, 2025 - While the shape tells you the dimensions of your array, the size gives you the total number of elements present in that array. The size attribute of a NumPy array provides this information, which is particularly useful when you need to know ...
Find elsewhere
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.ndarray.shape.html
numpy.ndarray.shape — NumPy v2.3 Manual
The shape property is usually used to get the current shape of an array, but may also be used to reshape the array in-place by assigning a tuple of array dimensions to it. As with numpy.reshape, one of the new shape dimensions can be -1, in which case its value is inferred from the size of the ...
🌐
AI Planet
aiplanet.com › learn › introduction-to-numpy › attributes-of-an-array-and-creating-basic-array › 89 › shape-size-and-data-type-of-an-array
Shape, Size and Data Type of an Array | AI Planet (formerly DPhi)
Here, ‘32’ is related to memory allocation. Note: If you have a 64-bit computer, dtype might be displayed as int64, and if you have 32-bit, it might be displayed as int32 · An array has an attribute called shape that tells you the number of items along each axis.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.ndarray.shape.html
numpy.ndarray.shape — NumPy v2.5.dev0 Manual
The shape property is usually used to get the current shape of an array, but may also be used to reshape the array in-place by assigning a tuple of array dimensions to it. As with numpy.reshape, one of the new shape dimensions can be -1, in which case its value is inferred from the size of the ...
🌐
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 - Imagine you’re trying to add two arrays. You assume they’re the same size, but surprise — they’re not! NumPy will throw an error, leaving you scratching your head. This is where numpy.shape steps in to save the day.
🌐
LinkedIn
linkedin.com › pulse › understanding-shape-dimension-compatibility-numpy-can-arslan
Understanding Shape and Dimension Compatibility in NumPy
March 31, 2023 - Understanding Shape and Dimension Compatibility in NumPy NumPy is a powerful numerical computing library in Python that enables users to perform mathematical and logical operations on large arrays and matrices. In NumPy, arrays and matrices have shapes and dimensions that determine how computations
🌐
Stack Overflow
stackoverflow.com › questions › 60988757 › numpy-dimensions-vs-shape
numpy: dimensions vs. shape - Stack Overflow
Would it not be necessary to have shapes with something like a m x n x o shape for a three-dimensional array? ... np.array can be any dimension you wish. np.matrix is a specialized 2-D array that retains its 2-D nature through operations. ... Yes to the second question. ... I think you are confusing things here, np.array can have any number of dimensions and np.matrix only two, np.matrix is the specific case where ndim=2 and have special matrix operators. From NumPy documentation of np.matrix:
🌐
NumPy
numpy.org › devdocs › user › quickstart.html
NumPy quickstart — NumPy v2.5.dev0 Manual
Note that numpy.array is not the ... array. This is a tuple of integers indicating the size of the array in each dimension. For a matrix with n rows and m columns, shape will be (n,m)....
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.ndarray.shape.html
numpy.ndarray.shape — NumPy v2.4 Manual
The shape property is usually used to get the current shape of an array, but may also be used to reshape the array in-place by assigning a tuple of array dimensions to it. As with numpy.reshape, one of the new shape dimensions can be -1, in which case its value is inferred from the size of the array and the remaining dimensions.
🌐
Python Guides
pythonguides.com › python-numpy-shape
NumPy Shape And Array Dimensions In Python
May 16, 2025 - The shape attribute returns a tuple showing the size of each dimension. For a 1D array, it’s simply the number of elements. For a 2D array, it’s (rows, columns). For higher dimensions, it follows the same pattern. ... Let’s use a more practical example.
🌐
Medium
medium.com › @techwithjulles › numpy-arrays-array-shape-and-dimensions-879b7592cf44
Numpy Arrays — Array Shape and Dimensions | by TechwithJulles | Medium
November 16, 2023 - Here, we used the .reshape() method ... shows us that the new array has a shape of (2, 3). The dimension of a NumPy array refers to the number of axes of the array....
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.ndarray.shape.html
numpy.ndarray.shape — NumPy v2.1 Manual
The shape property is usually used to get the current shape of an array, but may also be used to reshape the array in-place by assigning a tuple of array dimensions to it. As with numpy.reshape, one of the new shape dimensions can be -1, in which case its value is inferred from the size of the ...
Top answer
1 of 5
23

I wouldn't worry about performance here - any differences should only be very marginal.

I'd say the more pythonic alternative is probably the one which matches your needs more closely:

a.shape may contain more information than len(a) since it contains the size along all axes whereas len only returns the size along the first axis:

>>> a = np.array([[1,2,3,4], [1,2,3,4]])
>>> len(a)
2
>>> a.shape
(2L, 4L)

If you actually happen to work with one-dimensional arrays only, than I'd personally favour using len(a) in case you explicitly need the array's size.

2 of 5
12

From the source code, it looks like shape basically uses len(): https://github.com/pandas-dev/pandas/blob/master/pandas/core/frame.py

@property
def shape(self) -> Tuple[int, int]:
    return len(self.index), len(self.columns)
def __len__(self) -> int:
    return len(self.index)

Calling shape will attempt to run both dim calcs. So maybe df.shape[0] + df.shape[1] is slower than len(df.index) + len(df.columns). Still, performance-wise, the difference should be negligible except for a giant giant 2D dataframe.

So in line with the previous answers, df.shape is good if you need both dimensions, for a single dimension, len() seems more appropriate conceptually.

Looking at property vs method answers, it all points to usability and readability of code. So again, in your case, I would say if you want information about the whole dataframe just to check or for example to pass the shape tuple to a function, use shape. For a single column, including index (i.e. the rows of a df), use len().