Search code examples
python-3.xbuilt-in

Difference between len and size


I found two ways to determine how many elements are in a variable… I always get the same values for len () and size (). Is there a difference? Could size () have come with an imported library (like math, numpy, pandas)?

asdf = range (10)
print ( 'len:', len (asdf), 'versus size:', size (asdf) )

asdf = list (range (10))
print ( 'len:', len (asdf), 'versus size:', size (asdf) )

asdf = np.array (range (10))
print ( 'len:', len (asdf), 'versus size:', size (asdf) )

asdf = tuple (range (10))
print ( 'len:', len (asdf), 'versus size:', size (asdf) )

Solution

  • 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.