Digital Communication


Digital Communication

Python Array Length: find the length of an array


If you are working with arrays in Python and want to find their length, you have several options. We present two functions: len and size.

Find length of Python arrays with len

Since Python does not support arrays without importing additional librariesbut only Python lists, you need to embed the library first numpy in your project to be able to manipulate the data structure. You can then use arrays in Python. To know the number of elements contained in your array, you can use the standard Python function Len among others.

In the following example, we create an array that contains the numbers from 0 to 5.len call then makes sure that the length of our array is assigned to the variable l.

import numpy as np
a = np.array([0,1,2,3,4,5])
l = len(a)


The value 6 is now populated in the variable l because the array contains six elements in total.

The len function is also useful for finding the length of Python lists. Many programmers use these to replace arrays if no additional libraries should be used in the project.

Using Python for your web project? IONOS Deploy Now can help you deploy changes to your project in real time. Direct connectivity to Git simplifies your workflows and allows you to stay informed about the current status of your project at all times.

Find length of Python arrays with size

If you’re working with numpy, the library also offers a method to let you easily find the length of arrays. Named sizeshe is defined for arrays only: so it doesn’t work if you want to apply it to a Python list. Unlike len, size also lets you know the number of elements in a multidimensional array.

Also in this case, it is a good idea to rely on a code example to illustrate its use. First, we create the same array as in the first example, which contains the numbers from 0 to 5, and store the length in the variable named l. We then create an array which itself consists of three individual arrays and fill in its length in the variable called s.

import numpy as np
# array unidimensionnel
a = np.array([0,1,2,3,4,5])
l = a.size
# array multidimensionnel
m = np.array([[0,1,2], [3,4,5], [6,7,8], [9,10,11]])
s = m.size


If you consider the value entered in the variable l, you will notice that there is no difference with the call of len: in this case also, the variable l will contain a 6 because the array is composed of six elements.

The second size call may surprise you. A 12 is now stored in the variable named m. This stems from the fact that size counts the elements of all arrays one by one and displays them. A len call at this point returns you a 4 because thecall len always only relates to the first dimension of the array, where you store a total of four sub-arrays in this example. The elements that len ​​refers to are ” [0,1,2] », « [3,4,5] », « [6,7,8] ” And ” [9,10,11] “.

Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact