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

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().

Discussions

python - What is faster: Python3's 'len' or numpys shape? - Stack Overflow
I have an array of coordinates: points = [x,y] with the (numpy) dimensions/shape: (18, 1, 2) In matlab, to initialize an array of ones to index these points with '3', I could do this: A = ones(s... More on stackoverflow.com
🌐 stackoverflow.com
July 5, 2019
Is it ever advantageous to use a standard Python list vs a numpy array when all elements are the same type?
very often it is more efficient to use a list of numpy arrays than a higher dimensional (3D, 4D, ...) array. E.g., if you are computing polynomials, storing the terms in a list of arrays will be approximately sqrt(n_terms) faster than an nd array with the term # on the first dimension. The numpy allocator is relatively better at many small arrays than it is a few jumbo ones. More on reddit.com
🌐 r/Python
13
4
December 8, 2020
I want to know the length of a two-dimensional array
Assuming that every row has the same amount of elements you can get the length of the array with len(array) to get the amount of rows, and then get the number of columns by checking the length of the first row len(array[0]). Example array = [[1, 2, 3],[1, 2, 3]] print(len(array), len(array[0]) # outputs 2, the number of rows, and 3, the number of columns More on reddit.com
🌐 r/AskProgramming
6
0
December 24, 2021
Difference between .nunique() and .unique() please
Are you talking about Pandas or some other library? It's always best to specify if it's not standard Python. The Pandas docs have a small description and example that helps: DataFrame.nunique() Series.unique() Series.nunique() More on reddit.com
🌐 r/learnpython
6
1
May 9, 2023
People also ask

Does len() work on a NumPy array?
Yes, but len(arr) returns only the length of the first axis, not the total element count. For a multi-dimensional NumPy array, use arr.shape for the dimensions and arr.size for the total number of elements.
🌐
runxbuild.com
runxbuild.com › home › blog › python array length: len is the answer, and python lists are not arrays
Python Array Length: len Is the Answer, and Python Lists Are Not ...
Why is it len(x) and not x.length in Python?
Python uses a single built-in len() function that works across all container types, rather than each type carrying its own length method. Writing x.length or x.len() raises AttributeError. It is a deliberate design choice for consistency.
🌐
runxbuild.com
runxbuild.com › home › blog › python array length: len is the answer, and python lists are not arrays
Python Array Length: len Is the Answer, and Python Lists Are Not ...
Is len() slow on a large list?
No. len() is constant time. Python stores each object's length as a field and updates it on every change, so len() reads that field without counting elements. You can call it freely in loops and conditions without any performance concern.
🌐
runxbuild.com
runxbuild.com › home › blog › python array length: len is the answer, and python lists are not arrays
Python Array Length: len Is the Answer, and Python Lists Are Not ...
🌐
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.
🌐
Runebook.dev
runebook.dev › en › docs › numpy › reference › generated › numpy.ndarray.__len__
NumPy's len() vs. .shape and .size: A Guide to Array Dimensions
In the second example, len(arr_2d) returns 2, which is the number of rows, not the total number of elements or the number of columns. This is the most common trouble spot. Users often expect len() to give them the total number of elements. This is the classic mix-up. If you want the total count of all elements in the array, you should use the ndarray.size attribute.
🌐
Leapcell
leapcell.io › blog › understanding-array-length-in-python
Understanding Array Length in Python | Leapcell
July 25, 2025 - Use len() to get the length of Python lists and arrays. NumPy arrays offer .size for total element count.
Find elsewhere
🌐
RunxBuild
runxbuild.com › home › blog › python array length: len is the answer, and python lists are not arrays
Python Array Length: len Is the Answer, and Python Lists Are Not Arrays
July 18, 2026 - For a plain list, len is the whole story. For NumPy, len gives only the outer dimension, so reach for .shape or .size when you mean total elements.
🌐
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.
🌐
Delft Stack
delftstack.com › home › howto › numpy › numpy array length
How to Get NumPy Array Length | Delft Stack
March 11, 2025 - The two primary methods, numpy.size and numpy.shape, offer different insights: numpy.size gives you a total element count, while numpy.shape provides the dimensions of the array. Depending on your specific needs, you can choose the method that ...
🌐
Pierian Training
pieriantraining.com › home › python numpy tutorial: get length of array in python
Python NumPy Tutorial: Get Length of Array in Python - Pierian Training
April 27, 2023 - In this example, we created a one-dimensional NumPy array called `arr` and then used its `size` attribute to get the total number of elements in the array. Since `arr` has five elements, its size is also `5`. In conclusion, there are three ways to get the length of a NumPy array in Python: using the built-in function `len()`, using the `shape` attribute and accessing its first element, or using the `size` attribute.
🌐
Real Python
realpython.com › len-python-function
Using the len() Function in Python – Real Python
September 9, 2025 - You obtain the number of dimensions of a NumPy array either by using .shape and len() or by using the property .ndim. In general, when you have an array with any number of dimensions, len() returns the size of the first dimension:
🌐
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 - This code snippet demonstrates how to find the length of the first axis (number of rows) of a 2-dimensional array by using len() on the array’s shape. NumPy also offers a function called numpy.size() which can be used to find the number of elements along a specified axis.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.ndarray.size.html
numpy.ndarray.size — NumPy v2.5 Manual
Number of elements in the array · Equal to np.prod(a.shape), i.e., the product of the array’s dimensions
🌐
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.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › how to get numpy array length
How to Get NumPy Array Length - Spark By {Examples}
March 27, 2024 - In Python Numpy you can get array length/size using numpy.ndarray.size and numpy.ndarray.shape properties. The size property gets the total number of
🌐
Real Python
realpython.com › lessons › len-numpy-and-pandas
NumPy and Pandas (Video) – Real Python
In the previous lesson, I showed you some common coding cases of len(). In this lesson, I’ll show you how to use two third-party libraries and how they use len(). NumPy is a popular scientific calculation library for Python. It is written using…
Published: February 1, 2022
🌐
Udacity
udacity.com › blog › 2021 › 10 › how-to-calculate-the-length-of-an-array-in-python.html
How to calculate the length of an array in Python? | Udacity
October 20, 2021 - You can use the len() method for NumPy arrays, but NumPy also has the built-in typecode .size that you can use to calculate length.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.ndarray.size.html
numpy.ndarray.size — NumPy v2.6.dev0 Manual
Number of elements in the array · Equal to np.prod(a.shape), i.e., the product of the array’s dimensions