Reputation: 313
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) )
Upvotes: 10
Views: 23369
Reputation: 2431
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
.
Upvotes: 15
Reputation: 662
Consider this example :
a = numpy.array([[1,2,3,4,5,6],[7,8,9,10,11,12]])
print(len(a))
#output is 2
print(numpy.size(a))
#output is 12
len()
is built-in method used to compute the length of iterable python objects like str
, list
, dict
etc. len
returns the length of the iterable, i.e the number of elements. In above example the array is actually of length 2, because it is a nested list where each list is considered as an element.
numpy.size()
returns the size of the array, it is equal to n_dim1 * n_dim2 * --- n_dimn
, i.e it is the product of dimensions of the array, for example if we have an array of dimension (5,5,2), the size is 50, as it can hold 50 elements. But len()
will return 5, because the number of elements in higher order list (or 1st dimension is 5).
According to your question, len()
and numpy.size()
return same output for 1-D arrays (same as lists) but in vector form. However, the results are different for 2-D + arrays. So to get the correct answer, use numpy.size() as it returns the actual size.
When you callnumpy.size()
on any iterable, as in your example, it is first casted to a numpy array object, then size() is called.
Thanks for A2A
Upvotes: -1
Reputation: 178
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
Upvotes: 0