AppDividend
Latest Code Tutorials

# Python Array Size: How to Find Size of Array in Python

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

## Python array size

To find the size of an array in Python, use the len() method. The len() method returns the number of elements in the array.

`size = len(arr)`

The len() method takes a required parameter, which is an array or 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 len() function is doing behind the scenes is that it is calling the list.__len__(), and what len() really does is it takes the object and tries to call the objects’s __len__() method.

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

## Find the size of the Numpy array.

To find the size of a numpy array, use the “size” attribute. Numpy has size and shape attributes, but the size and shape attributes are not quite the same.

The size is an inbuilt 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`

The shape is an inbuilt 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.

## Conclusion

Python, unlike other programming languages like JavaScript or PHP, does not support a 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.

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