How To Find the Length of a List in Python
Last Updated :
26 Sep, 2024
The length of a list means the number of elements it has. The len() function is an inbuilt function in Python. It can be used to find the length of an object by passing the object within the parentheses. Below is a simple Python program to find length of a list using len()
Python
l1 = [10, 20, 30]
n = len(l1)
print("Length of l1 : ", n)
l2 = []
n = len(l2)
print("Length of l2 : ", n)
l3 = [[10, 20], ["gfg", "courses"], 10.5]
n = len(l3)
print("Length of l3: ", n)
OutputLength of l1 : 3
Length of l2 : 0
Length of l3: 3
We are now going to look at 7 other different methods to find the length of a list in Python.
Find the Length of a List Using 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.
Python
# Initializing list
test_list = [1, 4, 5, 7, 8]
# Printing test_list
print("The list is : " + str(test_list))
# Finding length of list using loop
# Initializing counter
counter = 0
for i in test_list:
# incrementing counter
counter = counter + 1
# Printing length of list
print("Length of list using naive method is : " + str(counter))
OutputThe list is : [1, 4, 5, 7, 8]
Length of list using naive method is : 5
Find the Length of a List Using a List Comprehension
Initialize a list called test_list with some values then Initialize a variable called length to 0. Use a List Comprehension to generate a sequence of ones for each element in the test_list.
This will create a list of ones with the same length as the test_list. Now use the sum() function to sum all the ones in the list generated by the list comprehension. Assign the sum to the length variable. Print the length variable.
Python
# Define the list to be used for the demonstration
test_list = [1, 4, 5, 7, 8]
# Calculate the length of the list using a list comprehension and the sum function
# The list comprehension generates a sequence of ones for each element in the list
# The sum function then sums all the ones to give the length of the list
length = sum(1 for _ in test_list)
# Print the length of the list
print("Length of list using list comprehension is:", length)
OutputLength of list using list comprehension is: 5
Time Complexity: The list comprehension creates a new list with a length equal to the length of the test_list. The sum() function then iterates over this list to compute the sum. Therefore, the time complexity of this algorithm is O(N), where N is the length of the test_list.
Auxiliary Space: The algorithm creates a new list of ones with a length equal to the length of the test_list using the list comprehension. Therefore, the auxiliary space complexity is also O(N), where N is the length of the test_list.
Find the Length of a List Using sum() Function
Use iteration inside the sum() and with each iteration adds one and at the end of the iteration, we get the total length of the list.
Python
# Initializing list
test_list = [1, 4, 5, 7, 8]
# Printing test_list
print("The list is : " + str(test_list))
# Finding length of list
# using sum()
list_len = sum(1 for i in test_list)
# Printing length of list
print("Length of list using len() is : " + str(list_len))
print("Length of list using length_hint() is : " + str(list_len))
OutputThe list is : [1, 4, 5, 7, 8]
Length of list using len() is : 5
Length of list using length_hint() is : 5
Find the Length of a List Using Recursion
We can use a Recursion function that takes a list lst as input and recursively calls itself, passing in a slice of the list that excludes the first element until the list is empty.
The base case is when the list is empty, in which case the function returns 0. Otherwise, it adds 1 to the result of calling the function on the rest of the list.
Python
# Define a function to count the number of elements in a list using recursion
def count_elements_recursion(lst):
# Base case: if the list is empty, return 0
if not lst:
return 0
# Recursive case: add 1 to the count of the remaining elements in the list
return 1 + count_elements_recursion(lst[1:])
# Test the function with a sample list
lst = [1, 2, 3, 4, 5]
print("The length of the list is:", count_elements_recursion(lst))
# Output: The length of the list is: 5
OutputThe length of the list is: 5
Find the Length of a List Using enumerate() function
Python enumerate() method adds a counter to an iterable and returns it in a form of an enumerating object.
Python
# python code to find the length
# of list using enumerate function
list1 = [1, 4, 5, 7, 8]
s = 0
for i, a in enumerate(list1):
s += 1
print(s)
Find the Length of a List Using Collections
Alternatively, you can also use the sum() function along with the values() method of the Collections Counter object to get the length of the list.
Python
from collections import Counter
# Initializing list
test_list = [1, 4, 5, 7, 8]
# Finding length of list using Counter()
list_len = sum(Counter(test_list).values())
print("Length of list using Counter() is:", list_len)
# This code is contributed by Edula Vinay Kumar Reddy
Find the Length of a List Using length_hint() Method
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. Here, we are finding length of list using len() and length_hint()
Python
from operator import length_hint
# Initializing list
test_list = [1, 4, 5, 7, 8]
# Printing test_list
print("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 list
print("Length of list using len() is : " + str(list_len))
print("Length of list using length_hint() is : " + str(list_len_hint))
OutputThe list is : [1, 4, 5, 7, 8]
Length of list using len() is : 5
Length of list using length_hint() is : 5
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.
Python
from operator import length_hint
import time
# Initializing list
test_list = [1, 4, 5, 7, 8]
# Printing test_list
print("The list is : " + str(test_list))
# Finding length of list
# using loop
# Initializing counter
start_time_naive = time.time()
counter = 0
for i in test_list:
# incrementing counter
counter = counter + 1
end_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 each
print("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)
OutputThe list is : [1, 4, 5, 7, 8]
Time taken using naive method is : 1.1920928955078125e-06
Time taken using len() is : 9.5367431640625e-07
Time taken using length_hint() is : 9.5367431640625e-07
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()
We have discussed 8 different methods to find the length of a list in Python. We have also done a performance analysis to check which method is the best.
You can use any of the above methods to find the length of a list. Finding list length is very useful when dealing with huge lists and you want to check the number of entries.