A simple way to get the length of any Python list is using the
len() function as follows:
This is a simple way to get the length of a Python list, let's dive into how the
len() function works and which other methods we can use to get the length of a list in Python in the following sections.
len() function is used to get the length of a Python list, you can pass a list to this function and it will return an integer indicating the size of the list as follows:
You can pass a list directly to this function like this:
Or you can also assign a list to a variable and pass that variable to the
len() function as follows:
It is important to clarify that a list length is not the highest accessible index of that list. Remember that the first item's index is 0, let's use the above example to explain this:
See that if we print index #2 of the names list we'll get the last item that is
Janet, but if we try to print index #3 (same value as the list length) we'll get an Index error indicating that the list index is out of range. So we can say that the last accessible index on a list would be the list length - 1.
length_hint() method is another way to get the length of any list in Python, it basically works in the same way as the
len() function, the difference, in this case, is that the
length_hint() method is defined in the operator module, this means that to use this method, we must import it first.
After importing this method into our code, to use
length_hint() we simply have to pass our list to it as follows
length_hint(any_list). Let's see an example of how to import and use this method as follows:
You can also use the
length_hint() method to check the length of a string as follows:
A third way to get the length of any list in Python is to use the Naive method. This method uses for loops to get the length of the desired list. To use this method, we have to follow some basic steps before getting the length of the desired list. First, we need to declare a counter variable and initialize this counter to 0. Second, we need to use a for loop to go through all elements of our list, with the condition to increase our counter variable by 1 each and every time the loop encounters an element of the list. Third, after looping through our list with the condition explained previously, our counter variable will give us the length of that list (we need to print the counter value to get the list length). Let's see an example of how to use the Naive method below:
In this example we first defined our list (
special_list), we then defined the counter variable and initialized it to 0 (
length), then we used a for loop to go through all the elements of the
special_list with the condition to increase our
length variable by 1 in this loop. Finally, we just needed to print our
length counter value to get the
As you can see, there are various ways how to get Python list length. If you are wondering which option to use, we recommend you stick with the
len() function since is the easiest and fastest way to get any list length. You'll also write less code and that is always a plus in the programming world since it is best to keep things simple.