. Advertisement .
..3..
. Advertisement .
..4..
How to get size of array python and python shape of array may sound like a daunting task at first, but there is also a chance that it can be equally as easy as pie in the meantime. How so? The plot twist depends mostly on your ways of dealing with such a problem.
For no longer struggling one fine day, read on to grab yourself the best optimum solutions!
Get Python Size Of Array
Little did you know, in Python, an array does not belong to a built-in data type. That is the reason trying to create this data makes absolutely no sense at all.
However, saying so doesn’t mean you can do nothing about it. As a flawless resolving key, a 3rd-party library like Numpy can be of great use to generate an array.
The Numpy.Size() Function
Now, if you are presumably referring to a Python list when mentioning arrays, so do we! Then the numpy.size() function would be your best bet to get things straight.
Running the code:
import numpy
array = numpy.array([[1, 2, 3], [3, 4, 5], [5, 6, 9]])
print("The size of a numpy array is: ", array.size)
Output:
The size of a numpy array is: 9
The Len() Function
Aside from the numpy.array() method, you can also utilize the len() function to determine an array’s length. This approach will accept an array as an input and output the number of entries in the array. And that’s how you can wind up whatever size you crave for!
# size = len("array")
cars = ['Bentley', 'Mercedes', 'Audi', 'Lexus', 'BMW', 'Ford']
print("The size of an array is: ", len(cars))
Output:
The size of an array is: 6
Get Python Shape Of Array
How about getting an array’s shape? The answer lies right in the simple yet effective numpy.shape() function.
By applying it in your code, the function will return you two elements in each array’s dimension. As such, the former is how many lists the array contains, and the latter is the total number of members present in each list.
Let’s take a look at the following example to make sure you’ve got a good hang of such a method.
Running the code:
import numpy
array = numpy.array([[12,28,22,29], [25,6,21,18], [21,33,5,25]])
print("The shape of a numpy array is: ", array.shape)
Output
The shape of an numpy array is: (3, 4)
In A Nutshell
Above is a full-package instruction regarding how to get size and shape of array in Python. Hopefully, you are no longer confused about figuring out the values needed.
Now that the questions poll has been puzzled out don’t forget to leave a comment if there is anything still perturbing you. See then!
Leave a comment