Python array size: get size of array in Python

Python array size

In this article, we will see how to get size of array in Python.

Python array size

An array stores a collection of similar elements in a contiguous memory location. In Python, we have a list, an iterable, and a numpy array to work as arrays.

We can perform various operations on arrays. The array size tells how many elements are present in the array. We can use the size of the array to calculate how much memory the array occupies by multiplying the size of the array with the size of individual elements.

Ways to calculate the size of array in Python

Let us now discuss the different methods available to calculate the size of array in Python.

Using the len() function

The len() function returns the length of an iterable like a list, numpy array, or string.

For example,

Output:

4 3

In the above example, we initialized a numpy array and a list and returned their length using the len() function.

There is a drawback to this method. If we have a multi-dimensional array, it will not return the size of the array. It will consider every array within the outer array to be a single element and return that number.

We can understand this better with an example as shown below.

Output:

2 2

In the above example, we can see that the len() functions return the length as 2 and do not count the elements of the inner arrays.

Using the numpy.size() attribute

The numpy arrays have an attribute called numpy.size(), which can return the size of the given array. This function overcomes the drawback of multi-dimensional arrays in the len() function.

See the code below.

Output:

4
6

As you can see, the above code returns the size of a numpy array. This method is not compatible with lists. To make it work with lists, we can convert the list to a numpy array using the numpy.array() function.

Using the numpy.shape() attribute

The numpy.shape() attribute returns the shape of the numpy array, which can be considered as the number of rows and columns of an array. We get the shape in a tuple.

We can multiply the numbers in the tuple to get the size of the given array. This method also is limited to numpy arrays.

We implement the above logic in the following example.

Output:

4
6

In the above example,

  • The shape() attribute returns the shape of the given array.
  • The numpy.prod() function multiplies all the elements to return the size of the array.

Conclusion

In this article, we discussed several ways to get the size of array in Python. The most simple method is using the len() function, but it has its drawbacks and does not work with multidimensional arrays. The numpy.size() and numpy.shape() methods overcome this array but lack compatibility with lists.

Related Posts

  • Python append to array
    30 December

    How to Append to Array in Python

    Table of ContentsWhat are the Different Array Implementations in Python?Python ListsPython ArraysPython Numpy ArraysAppend Element to List in pythonAppend to array in PythonAppend to NumPy array in pythonConclusion In python, we have three implementations of the array data structure. In this article, we will discuss those array implementations. After that, see how we can append […]

  • Python float array to int
    11 November

    Convert float array to int array in Python

    Table of ContentsWhat are the types of arrays?Ways to convert float arrays to int in PythonUsing the numpy.asarray() functionUsing the numpy.astype() functionUsing the numpy.int_() functionUsing the numpy.round_() functionUsing the numpy.rint() functionUsing the numpy.floor() functionUsing the numpy.ceil() functionUsing the numpy.trunc() functionConclusion What are the types of arrays? An array is a common structure that stores multiple […]

  • Convert Bytearray to String in Python
    01 November

    Convert Bytearray to String in Python

    Table of ContentsHow to create a bytearray from a string?Using the str() Function to convert Bytearray to String in PythonUsing the decode() Function to convert Bytearray to String in PythonUsing the codecs Module to convert Bytearray to String in PythonConclusion Python supports different types of sequence objects to store data. One such object is a […]

  • Inverse matrix in Python
    24 August

    Inverse matrix in python

    Table of ContentsUsing the Gauss-Jordan method to find the inverse of a given matrix in Python.Using the numpy.linalg.inv() function to find the inverse of a given matrix in Python. The inverse of a matrix in simple mathematics can be defined as a matrix, which when multiplied by the original matrix, gives out the Identity matrix. […]

  • Convert list to matrix in Python
    21 August

    Convert list to matrix in Python

    Table of ContentsUsing the while loop to convert list to matrix in Python.Use the numpy.array() method to convert list to matrix in Python.Using the numpy.asarray() method to convert list to matrix in Python. Python provides some in-built data types to stock multiple items in a single variable. A List is one among these built-in data […]

  • Rotate image Python
    20 August

    Rotate image in Python

    Table of ContentsUsing the image.rotate() function from the Pillow Imaging Library (Fork) to rotate image in Python.Using NumPy library functions to rotate image in Python.Using the OpenCV and imutils library functions to rotate image in Python.Using matplotlib and SciPy libraries to rotate image in Python. Python supports the use of images and sometimes a given […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.