Introduction to Python List Length
In this article, we are discussing the length function on the list in Python. In Python, the list is a collection of elements of different data types. In this article, we want to find the number of elements in the list. Doing this in Python provides an inbuilt function known as length and is represented as len(). The len() method in Python finds the size of the list, which returns the number of elements in the given list. This len() method accepts the iterable as an argument to count the number of items in the given list. In Python, there are different ways of finding the length of the list. Let us see in detail in the below section.
How does Python List Length work?
This section will see the different ways of finding the length of the list in Python. The length of the list means to find the size of the length or number of elements in the given list. Let us see different ways of finding the length of the list are:
1. Using len() method
This len() method is the inbuilt function in Python for finding the length of the list or any other iterable. This method takes any iterable as a parameter, and it only returns the number of elements in that iterable passed to this method. This method is the most commonly used and easy method for finding the length of the list.
Syntax:
len(listname)
listname: this is the name of any iterable to find the length or size of the given iterable.
Code:
print("Program to demonstrate working of len() method:") print("\n") lst = ["Educba", "Training", 1, 2, 3, 5] print("The given list is as follows:") print(lst) print("\n") print("The length of the given list is as follows: ") print(len(lst))
Output:
In the above program, we can see we have declared a list with the name “lst”, and we are printing the size of this list using the len() method, which is used as len(last), which returns the output as “6” as there are 6 elements in the list “lst”.
2. By using the Naive method
This is another basic method for finding the length of the list. This method runs a loop, and the counter is incremented until the last element of the list is found. This is the most basic method used when the other techniques are not efficient.
Code:
print("Program to demonstrate length of list using naive method:") print("\n") lst = [ 1, 5, 7, 5, 2 ] print ("The given list is as follows:") print(lst) print("\n") counter = 0 for i in lst: counter = counter + 1 print ("The Length of given list using naive method is as follows:") print(counter)
Output:
In the above program, we have seen to find the length of the list, we are traversing the list and counting the elements, and then the count is stored in the counter which prints the length of the list. This method is also known as using a “for” loop as we use for loop to find the length of the given list.
3. By using the length_hint() method
This is another method for finding the length of the list. This method is not much popular as the above two methods. This method is defined in the operator class and which helps to find the number of elements in the present list. This is another inbuilt function to count the number of elements in the list, and this method can also be applied on any iterable such as list, tuple, dict, etc. Let us see the example below demonstrating the length_hint() method.
Syntax:
length_hint(iterable)
Iterable: it can be any iterable such as list, tuple, dict, etc., which is passed as a parameter to find the number of elements.
Code:
from operator import length_hint print("Program to demonstrate the length of the list using length_hint() method:") print("\n") lst = ['Educba','Training','1','4', 'Python','Courses'] print("The given list is as follows:") print(lst) print("\n") length_lst = length_hint(lst) print("Length of the given list is as follows:") print(length_lst)
Output:
Explanation: In the above program, we can see we have declared a list with the name as “lst”, and we have printed the contents of the list. Then we are applying the length_hint() method by passing “lst” to this method, and the result is stored in another variable known as “length_lst”, which gives the output as 6. This output with the program can be seen in the above screenshot.
In the all above 3 methods of finding the length of the list are seen clearly with examples. But most of the programmers prefer to use len() method than the other two methods as this len() method is faster in performing the count of the elements in the list than the other two lists. Let us see the performance of all three methods by resulting in the time of performing these methods, and we can find that len() method is better to use than the other two methods.
Example to Implement Python List Length
Below are the examples mentioned:
Code:
print("Program to demonstrate length of list by all three methods with their Performances:") print("\n") from operator import length_hint import time lst = [ 1, 3, 5, 7, 9, 11 ] print ("The given list is as follows: ") print(lst) print("\n") start_time_naive = time.time() counter = 0 for i in lst: counter = counter + 1 end_time_naive = str(time.time() - start_time_naive) start_time_len = time.time() list_len = len(lst) end_time_len = str(time.time() - start_time_len) start_time_hint = time.time() list_len_hint = length_hint(lst) end_time_hint = str(time.time() - start_time_hint) print ("Time taken by using naive method is : " + end_time_naive) print ("Time taken by using len() method is : " + end_time_len) print ("Time taken by using length_hint() method is : " + end_time_hint)
Output:
In the above program, we can see each method’s time to count the number of elements in the given list.
Conclusion
In this article, we conclude that finding the length of the list in Python is very easy, and there are different ways of finding the length or size of the given list. In this article, we saw that the most common and easy method is the len() method, an inbuilt function in Python to find the length of the list. The second method is using the “for” loop or using the naïve method, and the last method is the length_hint() method for finding the number of items present in the present list. At last, we also saw which method is better for using to find the length of the list in Python.
Recommended Articles
This is a guide to Python List Length. Here we discuss an introduction to Python List Length with syntax, working and programming examples. You can also go through our other related articles to learn more –