How to find out the length of a Python list

You can use the len or length_hint function to find out the length of a Python list. If you want to avoid using functions, you can opt to use different Python loops.

Option 1: Use Python len

The most popular way to find out a Python list length is to use the Python len function. This is defined for sequences, such as lists, and collection types. It can also be used to find out one-dimensional Python array lengths. To do so, it uses Python objects. The return value is an integer that represents the number of elements in an object.

Here’s an example that shows how to use Python len:

l = ["python", "is", "a", "versatile", "language"]
len(l)
python

A list called l is created, which contains five words, or Python strings. This list is transferred when executing len in the second line of the Python code. Since there are five elements in the list, you should get a result of “5” from the len function.

Tip

Using Python for your web project? With IONOS Deploy Now, you always have a clear overview of your web project and can deploy any changes directly in GitHub.

Option 2: Use a loop

You can also find out the length of a Python list by using a loop of your choosing and iterating each element. A while loop increases the count with each loop and removes an element from your list (or a copy). A for loop, on the other hand, automatically loops every element in your Python list. Here you will need an additional counter, which you can use to increment in the loop and find out the Python list length.

While loop

In the following example, we’ll look at a Python while loop, which takes elements from your lists and counts how many elements were originally in the list.

l = ["python", "is", "a", "versatile", "language"]
count = 0
while (l):
	count += 1
	l.pop()
python

Once again, we’ve created a list called l. In addition to this, there is a variable called count, which starts counting from the value 0. Now we can use a while loop, which will run through the list. In each iteration, the variable count is increased by 1. On top of this, the pop() function is executed in order to delete the last element from the list. This is done to ensure that the loop will end at some point.

For loop

Instead of using a while loop, you can also use a Python for loop to find out the number of elements in a list. With this loop, you don’t need the pop() function since the elements in the list do not need to be deleted.

l = ["python", "is", "a", "versatile", "language"]
count = 0
for element in l:
	count += 1
python

When using the for loop, every element in the list is taken into account. This means that the number of loops counted using the count variable corresponds to the number of elements within a list.

Option 3: Use the length_hint function

A lesser-known Python function, which is implemented in an additional Python module called operator, is length_hint.

To use this option, you’ll need to first import the function into your code.

from operator import length_hint
python

length_hint is executed with the same syntax as Python len and will give the same results.

l = ["python", "is", "a", "versatile", "language"]
length_hint(l)
python

Since there are five elements in the l list, the function delivers back the value “5”.

Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top