Python array length: How to find Python size of array

0
2142
Python Array Size - How to Find Size of Array in Python

Python has no built-in array data type. But you can create an array using a third-party library like Numpy. To clear at the beginning, by array, you probably mean a list in Python. Then, to find the size of the list, use the len() method.

Python array length

To find a length of an array in Python, use the len() method. The len() is a built-in Python method that takes an array as an argument and returns the number of elements in the array. The len() function returns the size of an array.

size = len(arr)

The len() method takes a required parameter, an array, or a list.

# app.py

numbers = [11, 21, 19, 18, 46]

print("The size of an array is: ", len(numbers))

Output

The size of an array is:  5

The size of an array is always one more than the highest array index. For example, the index of the last element of the array is 4; the size of an array is 5. Always +1 of the last index of the element.

What the len() function is doing behind the scenes is that it is calling the list.__len__(), and what len() does is it takes the object and tries to call the objects __len__() method.

In short, the len() function works only on objects with a __len__() method. If you have a custom object with the __len__() method, you can even call len().

Python size of an array

To find the size of an array, use the numpy size property. The numpy array has size and shape attributes, but the size and shape attributes are not quite the same. The size is a built-in attribute that returns the size of the array.

# app.py

import numpy as np

arr = np.array([[1, 2], [3, 4], [5, 6]])

print("The size of a numpy array is: ", arr.size)

Output

The size of a numpy array is:  6

In the above code, we get the number of elements in the array with the numpy.size property. The size property works great with one-dimensional arrays. It does not consider the multi-dimensional arrays; it only gives us the number of elements in an array.

The shape is a built-in attribute that returns the shape of the array. It counts the number of elements of the array and then returns it.

# app.py

import numpy as np

arr = np.array([[1, 2], [3, 4], [5, 6]])

print("The shape of a numpy array is: ", arr.shape)

Output

The shape of an numpy array is:  (3, 2)

It returns the shape of the array.

numpy array length

To find the numpy array length in Python, use the ndarray.size property. The np.ndarray.size attribute returns several elements in the array. The numpy.shape property returns a tuple in the form of (a, b), where a is the number of rows in the array and b is the number of columns in the array.

# app.py

import numpy as np

np_arr = np.arange(3)
print(np_arr)
print(np_arr.shape)

Output

[0 1 2]
(3,)

The shape (= length of each dimension) of numpy.ndarray can be accepted as a tuple with attribute shape.

In our case, an array is a one-dimensional array, and that’s why it returns a tuple with one element instead of an integer value.

numpy size of 2d array

To get the length of a 2D array in Python numpy, use the array.size attribute. The array.size attribute returns the number of elements in the array.

# app.py

import numpy as np

np_arr = np.array([[11, 21], [31, 41]])
print(np_arr)
print(np_arr.shape)

Output

[[11 21]
[31 41]]

(2, 2)

You can see that it is equivalent to np.prod(a.shape), i.e., the product of the array’s dimensions.

Conclusion

Python, unlike other programming languages like JavaScript or PHP, does not support normal “length()” or “size()” functions to get the size of an array. For example, Instead of “array.length()“, use the len() function, which takes the array itself as a parameter and returns its size.

That’s it for this article.

See also

Python list length

Python array contains

Python array append

Python array insert

Python string length

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.