A List in Python is implement to store the sequence of various types of data. It is a collection data-type that is ordered and changeable. A list can have duplicate values as well. Here, Python has three methods to find the length of list.
Methods to find Length of List in Python
There are three methods that are used to find the length of the list in Python:
- len()
- Naive Method
- length_hint()
len() Method
There is a build-in function called len() for getting the total number of elements in a list. The len() method takes argument where you may provide a list and returns the length of the given list. It is the most convenient ways to find the length of list in Python.
#Finding length of list by using len() method
list_example = [1, 2, 3, 4, 5,6,7,8,9]
print("The length of list is: ", len(list_example))
The output will be:
Output:
The length of list is: 9
Naive Method
Without using in-build function len(), length of list can be easily found using for loop. This method is called as Naive method. It can be summarized as follows
- Firstly, declare a counter variable and initialize it to zero.
- Using for loop,traverse through all the data elements and after encountering every element,increment the counter variable by 1.
- Now, the length of the array will be stored in the counter variables and it will represent the number of elements in the list.
#Finding length of list by using Naive method
#Initializing list
list_example = [1, 2, 3, 4, 5,6,7,8,9]
print("The list is: ", str(list_example))
#finding length of list using loop
counter = 0
for i in list_example:
counter = counter + 1
print ("Length of list using Naive Method is: ", str(counter))
length_hint() Method
This method is defined in operator class and it can also define the length of list.
#Using Length_hint() Method
from operator import length_hint
list_len_1 = [1, 2, 3, 5, 6, 'PickupBrain']
list_len_hint = length_hint(list_len_1)
print(list_len_hint)
Performance test of len(), Naive Method and lenght_list Methods
This time analysis will help to understand how much time it takes to execute all the methods, which will help you to chose one over another.
#Code for Performance Analysis
from operator import length_hint
import time
#define list
list_example = [1, 2, 3, 4, 5,6,7,8,9]
print("The list is: ", list_example)
# Using Naive method & loop to find length of list
# Initializing counter
start_naive_time = time.time()
counter = 0
for i in list_example:
counter = counter + 1
end_naive_time = float(time.time() - start_naive_time)
# Using len() method
start_len_time = time.time()
list_len = len(list_example)
end_len_time = float(time.time()- start_len_time)
# Using length_hint() method
start_hint_time = time.time()
list_hint = length_hint(list_example)
end_hint_time = float(time.time()- start_hint_time)
#Printing time for each method
print("Time taken by Naive Method: ", end_naive_time)
print("Time taken by Length_hint() Method: ", end_len_time)
print("Time taken by len() Method: ", end_hint_time)
The output will be this:
Output:
The list is: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Time taken by Naive Method: 3.0994415283203125e-06
Time taken by Length_hint() Method: 4.76837158203125e-07
Time taken by len() Method: 1.1920928955078125e-06
Cheat Sheet for Length of list in Python
Conclusion
The time taken is
Naive Method >> len() >length_hint()
C P Gupta is a YouTuber and Blogger. He is expert in Microsoft Word, Excel and PowerPoint. His YouTube channel @pickupbrain is very popular and has crossed 9.9 Million Views.