AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

Python List Length: find it easily

PARTAGEZ

You can find the length of Python lists by calling the len or length_hint functions. If you want to give up using functions, the various loops in Python will help you.

Variant 1: Python len

Arguably the best known and easiest way to find the Python List Length is to use the Python len function. It is defined for sequence and collection types, including lists, and also used to find the Python Array Length of one-dimensional arrays. It accepts a Python object as a transfer parameter. The returned value is an integer value representing the number of elements in the object.

A sample code illustrates the use of Python len:

l = ["python", "est", "un", "langage", "polyvalent"]
len(l)

python

A list called “l” was created first, it contains a total of five words commonly referred to as Python strings. This list is passed to the Python function len in the second line of code in the function call. In this case, then returns the integer value “5”, since a total of five items are stored in the list.

Using Python for your web project? With Deploy Now, you can very easily stay in control of your web project and deploy all changes to your code directly via GitHub!

Variation 2: Loops

You can also determine Python List Length by iterating over each element of the list with a loop of your choice. Besides the while loop, where you increment a counter with each loop execution and remove an item from your list (or a copy of it), you can also use a for loop which automatically iterates over each item in your Python list once. Also here you need an extra counter that you increment with each loop execution to find the length of the Python list.

while loop

In this sample code, we first look at a Python while loop, which removes all the items from your list while counting the number of items originally contained in the list:

l = ["python", "est", "un", "langage", "polyvalent"]
count = 0
while (l):
	count += 1
	l.pop()

python

We again create a list in the code called “l”. In addition, a variable named count, which should act as a counter, is initialized with the value 0. A while loop is now launched, which traverses the list as long as it still contains elements. The count variable is incremented by 1 on each iteration of the loop. The pop() function is also called on the list, which removes the last item from the list. This ensures that the loop ends at some point.

for loop

Instead of a while loop, a for loop in Python can also be used to find out the number of elements in a Python list. This saves you a line of code, because you can skip removing items from the list:

l = ["python", "est", "un", "langage", "polyvalent"]
count = 0
for element in l:
	count += 1

python

The for loop examines each item in the list once, so the number of loop executions, which are counted with the variable named count, equals the number of items in the list.

Variant 3: length_hint function

A lesser known Python function implemented in an additional Python module called “operator” is length_hint.

First, you need to import the function into your code:

from operator import length_hint

python

The call of length_hint is structured the same as calling Python len and therefore returns the same result.

l = ["python", "est", "un", "langage", "polyvalent"]
length_hint(l)

python

Since there are still five elements in the list named “l”, the function returns a value of 5 at this point.

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