size comes from numpy (on which pandas is based).

It gives you the total number of elements in the array. However, you can also query the sizes of specific axes with np.size (see below).

In contrast, len gives the length of the first dimension.

For example, let's create an array with 36 elements shaped into three dimensions.

In [1]: import numpy as np                                                      

In [2]: a = np.arange(36).reshape(2, 3, -1)                                     

In [3]: a                                                                       
Out[3]: 
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]]])

In [4]: a.shape                                                                 
Out[4]: (2, 3, 6)

size

size will give you the total number of elements.

In [5]: a.size                                                        
Out[5]: 36

len

len will give you the number of 'elements' of the first dimension.

In [6]: len(a)                                                                  
Out[6]: 2

This is because, in this case, each 'element' stands for a 2-dimensional array.

In [14]: a[0]                                                                   
Out[14]: 
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17]])

In [15]: a[1]                                                                   
Out[15]: 
array([[18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35]])

These arrays, in turn, have their own shape and size.

In [16]: a[0].shape                                                             
Out[16]: (3, 6)

In [17]: len(a[0])                                                              
Out[17]: 3

np.size

You can use size more specifically with np.size.

For example you can reproduce len by specifying the first ('0') dimension.

In [11]: np.size(a, 0)                                                          
Out[11]: 2

And you can also query the sizes of the other dimensions.

In [10]: np.size(a, 1)                                                          
Out[10]: 3

In [12]: np.size(a, 2)                                                          
Out[12]: 6

Basically, you reproduce the values of shape.

Answer from nocibambi on Stack Overflow
Top answer
1 of 3
17

size comes from numpy (on which pandas is based).

It gives you the total number of elements in the array. However, you can also query the sizes of specific axes with np.size (see below).

In contrast, len gives the length of the first dimension.

For example, let's create an array with 36 elements shaped into three dimensions.

In [1]: import numpy as np                                                      

In [2]: a = np.arange(36).reshape(2, 3, -1)                                     

In [3]: a                                                                       
Out[3]: 
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]]])

In [4]: a.shape                                                                 
Out[4]: (2, 3, 6)

size

size will give you the total number of elements.

In [5]: a.size                                                        
Out[5]: 36

len

len will give you the number of 'elements' of the first dimension.

In [6]: len(a)                                                                  
Out[6]: 2

This is because, in this case, each 'element' stands for a 2-dimensional array.

In [14]: a[0]                                                                   
Out[14]: 
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17]])

In [15]: a[1]                                                                   
Out[15]: 
array([[18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35]])

These arrays, in turn, have their own shape and size.

In [16]: a[0].shape                                                             
Out[16]: (3, 6)

In [17]: len(a[0])                                                              
Out[17]: 3

np.size

You can use size more specifically with np.size.

For example you can reproduce len by specifying the first ('0') dimension.

In [11]: np.size(a, 0)                                                          
Out[11]: 2

And you can also query the sizes of the other dimensions.

In [10]: np.size(a, 1)                                                          
Out[10]: 3

In [12]: np.size(a, 2)                                                          
Out[12]: 6

Basically, you reproduce the values of shape.

2 of 3
0

Numpy nparray has Size https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.size.html

Whilst len is from Python itself

Size is from numpy ndarray.size

The main difference is that nparray size only measures the size of an array, whilst python's Len can be used for getting the length of objects in general

🌐
CASRAI
casrai.org › guides › research tools & software › statistical software for research › numpy ndarray length: len(), .shape and .size explained
NumPy ndarray Length: len, shape, size — CASRAI
2 weeks ago - Use len(a) for the first-axis length, a.size for the total number of elements, or a.shape for the length of every axis. For a one-dimensional array they describe the same count. Length (len()) is the number of items along the first axis. Size (.size) is the number of elements across all axes, equal to np...
Discussions

python - shape vs len for numpy array - Stack Overflow
CopyIn [17]: a = np.random.randint(0,10000, size=1000000) In [18]: %time a.shape CPU times: user 6 µs, sys: 2 µs, total: 8 µs Wall time: 13.1 µs Out[18]: (1000000,) In [19]: %time len(a) CPU times: user 5 µs, sys: 1 µs, total: 6 µs Wall time: 9.06 µs Out[19]: 1000000 · So, what is the difference and which one is more pythonic... More on stackoverflow.com
🌐 stackoverflow.com
python - shape vs len for numpy array - Stack Overflow
In [17]: a = np.random.randint(0,10000, size=1000000) In [18]: %time a.shape CPU times: user 6 µs, sys: 2 µs, total: 8 µs Wall time: 13.1 µs Out[18]: (1000000,) In [19]: %time len(a) CPU times: user 5 µs, sys: 1 µs, total: 6 µs Wall time: 9.06 µs Out[19]: 1000000 · So, what is the difference and which one is more pythonic... More on stackoverflow.com
🌐 stackoverflow.com
December 27, 2019
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
How do I find the length (or dimensions, size) of a numpy matrix in python? - Stack Overflow
However, this feels unsatisfying ... return the number of rows. It actually works differently than len([1,2]) would for a 2D python array, as this would return 2. So, is there a more intuitive way to find the size of a matrix, or is this the best I have?... More on stackoverflow.com
🌐 stackoverflow.com
People also ask

What is the difference between numpy ndarray size and length?
Length ( len() ) is the number of items along the first axis. Size ( .size ) is the number of elements across all axes, equal to np.prod(a.shape) . A 3 x 5 x 2 array has length 3 and size 30.
🌐
casrai.org
casrai.org › guides › research tools & software › statistical software for research › numpy ndarray length: len(), .shape and .size explained
NumPy ndarray Length: len, shape, size — CASRAI
What does numpy.ndarray length return for a 2-D array?
The number of rows. A 2 x 3 array returns 2; use a.shape[1] for the 3 columns or a.size for all 6 elements.
🌐
casrai.org
casrai.org › guides › research tools & software › statistical software for research › numpy ndarray length: len(), .shape and .size explained
NumPy ndarray Length: len, shape, size — CASRAI
How do I find the length of a NumPy array?
Use len(a) for the first-axis length, a.size for the total number of elements, or a.shape for the length of every axis. For a one-dimensional array they describe the same count.
🌐
casrai.org
casrai.org › guides › research tools & software › statistical software for research › numpy ndarray length: len(), .shape and .size explained
NumPy ndarray Length: len, shape, size — CASRAI
🌐
Runebook.dev
runebook.dev › en › docs › numpy › reference › generated › numpy.ndarray.__len__
NumPy's len() vs. .shape and .size: A Guide to Array Dimensions
The .size attribute gives you the product of all dimensions, which is the total element count. Sometimes you need the length of a dimension other than the first one (e.g., the number of columns in a 2D array). Using len() won't work for this.
🌐
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 - 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.
🌐
Medium
medium.com › @heyamit10 › numpy-ndarray-size-931a5805aedb
Understanding ndarray.size. If you think you need to spend $2,000… | by Hey Amit | Medium
February 8, 2025 - This makes .size far more versatile, especially for multidimensional arrays. Imagine working with a 3D array—len() won’t tell you the whole story, but .size will.
🌐
Leapcell
leapcell.io › blog › understanding-array-length-in-python
Understanding Array Length in Python | Leapcell
July 25, 2025 - Here, len(np_array) returns the size of the first dimension (i.e., the number of rows), while .size gives the total number of elements in the entire array. The .shape attribute provides the dimensions of the array.
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().

Find elsewhere
🌐
IONOS
ionos.com › digital guide › websites › web development › python array length
How to find out the length of a Python array - IONOS
July 11, 2023 - If you work with NumPy, the library also has a way to find out Python array length. It’s called size and is defined only for arrays, meaning if you want to use it for Python lists, it won’t work.
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().

🌐
Appdividend
appdividend.com › python-array-length
How to Get the Length of an Array in Python
September 7, 2025 - If you are working with a list or array's array structure, use the len() function. For numpy arrays, use the .size attribute.
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to determine the length of a numpy array
5 Best Ways to Determine the Length of a NumPy Array - Be on the Right Side of Change
February 20, 2024 - The snippet uses the np.size() function on a 3×2 array without specifying an axis, which results in the total number of elements in the array being calculated and printed. Method 1: size Attribute. Provides total element count. Simple to use but not axis-specific. Method 2: shape and Indexing. Allows axis-specific sizes. Requires indexing, which might be less intuitive for total size. Method 3: len() Function.
🌐
Codegive
codegive.com › blog › numpy_length_of_an_array.php
Numpy length of an array
For a one-dimensional (1D) NumPy array, len() behaves exactly as you'd expect: it returns the number of elements in that array. # Create a 1D array arr_1d = np.array([10, 20, 30, 40, 50]) print(f"1D Array: {arr_1d}") print(f"Length of 1D array using len(): {len(arr_1d)}") # Output: 5
🌐
Python Guides
pythonguides.com › check-the-length-of-an-array-in-python
How To Check The Length Of An Array In Python?
July 22, 2026 - A dimension is an axis, such as rows or columns in an Excel sheet. For a one-dimensional NumPy array, len() returns the number of elements: import numpy as np leave_hours = np.array([8, 4, 8, 16, 8]) print(len(leave_hours))
🌐
Python Guides
pythonguides.com › python-numpy-shape
NumPy Shape in Python: shape[0] and shape[1]
May 8, 2025 - Because arr.shape is already a tuple and the parentheses try to call it. Drop them. If you want a function, np.shape(arr) is a real function and accepts lists too. shape is the tuple of dimension lengths and size is the single total number of elements, equal to the product of that tuple.
🌐
Programmersought
programmersought.com › article › 3539725621
The difference between size, shape and len in python-numpy - Programmer Sought
Numpy array type The output is: For Numpy Array, Shape, Size is the properties of Array; Pytorch Tensor Type The output is: For Tensor, Shape is the properties of Tensor; Size () is a method of tensor... len() Can be used to view the length of an array or slice cap()Can be used to view the ...