There are several techniques you can use in Python to find the length of a list. The length of a list is the number of elements in the list.
To get the length of a list in Python, the most common way to do so is to use the len()
method as it’s the most efficient and is a built-in function. Since a list is an object, the size of the list if already stored in memory for quick retrieval.
We have also included two other methods you can use to find the length of a list with python.
len()
method to get the length of a listYou can use the built-in len()
method to find the length of a list.
The len()
method accepts a sequence or a collection as an argument and returns the number of elements present in the sequence or collection.
The syntax of len()
is:
len(s)
The following example provides a list and uses the len()
method to get the length of the list:
inp_lst = ['Python', 'Java', 'Ruby', 'JavaScript']
size = len(inp_lst)
print(size)
The output is:
Output4
Although the len()
method is usually the best approach to get the length of a list because it’s the most efficient, there are a few other ways to find the length of a list in Python.
length_hint()
method to get the length of a listThe Python operator
module has a length_hint()
method to estimate the length of a given iterable object. If the length is known, the length_hint()
method returns the actual length. Otherwise, the length_hint()
method returns an estimated length. For lists, the length is always known, so you would normally just use the len()
method.
The syntax of length_hint()
is:
length_hint(object)
The following example provides a list and uses the length_hint()
method to get the length of the list:
from operator import length_hint
inp_lst = ['Python', 'Java', 'Ruby', 'JavaScript']
size = length_hint(inp_lst)
print(size)
The output is:
Output4
for
loop to get the length of a listThis section provides a less practical, but still informative, way of finding the length of a list with no special method. Using a python for
loop to get the list length is also known as the naive method and can be adapted for use in almost any programming language.
The basic steps to get the length of a list using a for
loop are:
Declare a counter variable and initialize it to zero.
counter = 0
Use a for
loop to traverse through all the data elements and, after encountering each element, increment the counter variable by 1.
for item in list:
counter += 1
The length of the array is stored in the counter variable and the variable represents the number of elements in the list. The variable can be used in other code or output.
print(counter)
The following example demonstrates how to get the length of a list:
inp_lst = ['Python', 'Java', 'Ruby', 'JavaScript']
size = 0
for x in inp_lst:
size += 1
print(size)
The output is:
Output4
In this article, you learned some different ways to find the length of a list in Python. Continue your learning with more Python tutorials.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.