Python | Ways to find length of list

22 Jun, 2022
List being an integral part of Python day-to-day programming has to be learned by all Python users and having a knowledge of its utility and operations is essential and always a plus. So this article discusses one such utility of finding the no. of elements in a list. 
Method 1: Naive Method
In this method, one just runs a loop and increases the counter till the last element of the list to know its count. This is the most basic strategy that can be possibly employed in the absence of other present techniques.Code #1 : Demonstrating finding length of a list using the Naive Method
Python3
# Python code to demonstrate# length of list# using naive method # Initializing listtest_list = [ 1, 4, 5, 7, 8 ] # Printing test_listprint ("The list is : " + str(test_list)) # Finding length of list# using loop# Initializing countercounter = 0for i in test_list:         # incrementing counter    counter = counter + 1 # Printing length of listprint ("Length of list using naive method is : " + str(counter))
Output : 
The list is : [1, 4, 5, 7, 8]
Length of list using naive method is : 5
Method 2 : Using len()
The len() method offers the most used and easy way to find the length of any list. This is the most conventional technique adopted by all programmers today.
Python3
# Python program to demonstrate working# of len()a = []a.append("Hello")a.append("Geeks")a.append("For")a.append("Geeks")print("The length of list is: ", len(a))
The length of list is:  4
 
Python3
# Python program to demonstrate working# of len()n = len([10, 20, 30])print("The length of list is: ", n)
The length of list is:  3
 
Method 3 : Using length_hint()
This technique is a lesser-known technique for finding list length. This particular method is defined in the operator class and it can also tell the no. of elements present in the list. Code #2 : Demonstrating finding length of list using len() and length_hint() 
Python3
# Python code to demonstrate# length of list# using len() and length_hintfrom operator import length_hint # Initializing listtest_list = [ 1, 4, 5, 7, 8 ] # Printing test_listprint ("The list is : " + str(test_list)) # Finding length of list# using len()list_len = len(test_list) # Finding length of list# using length_hint()list_len_hint = length_hint(test_list) # Printing length of listprint ("Length of list using len() is : " + str(list_len))print ("Length of list using length_hint() is : " + str(list_len_hint))
Output : 
The list is : [1, 4, 5, 7, 8]
Length of list using len() is : 5
Length of list using length_hint() is : 5
Performance Analysis – Naive vs len() vs length_hint()
When choosing amongst alternatives it’s always necessary to have a valid reason why to choose one over another. This section does a time analysis of how much time it takes to execute all of them to offer a better choice to use.Code #3: Performance Analysis 
Python3
# Python code to demonstrate# length of list# Performance Analysisfrom operator import length_hintimport time # Initializing listtest_list = [ 1, 4, 5, 7, 8 ] # Printing test_listprint ("The list is : " + str(test_list)) # Finding length of list# using loop# Initializing counterstart_time_naive = time.time()counter = 0for i in test_list:         # incrementing counter    counter = counter + 1end_time_naive = str(time.time() - start_time_naive) # Finding length of list# using len()start_time_len = time.time()list_len = len(test_list)end_time_len = str(time.time() - start_time_len) # Finding length of list# using length_hint()start_time_hint = time.time()list_len_hint = length_hint(test_list)end_time_hint = str(time.time() - start_time_hint) # Printing Times of eachprint ("Time taken using naive method is : " + end_time_naive)print ("Time taken using len() is : " + end_time_len)print ("Time taken using length_hint() is : " + end_time_hint)
Output : 
The list is : [1, 4, 5, 7, 8]
Time taken using naive method is : 2.6226043701171875e-06
Time taken using len() is : 1.1920928955078125e-06
Time taken using length_hint() is : 1.430511474609375e-06
In the below images, it can be clearly seen that time taken is naive >> length_hint() > len(), but the time taken depends highly on the OS and several of its parameter. In two consecutive runs, you may get contrasting results, in fact sometimes naive takes the least time out of three. All the possible 6 permutations are possible.
  naive > len() > length_hint()
naive > len()=length_hint() 
naive > length_hint() >len() 
naive > length_hint()  > len()
Spider_man
farzams101
Python list-programs
python-list
Python
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.