The list in python can be defined as a collection of ordered and changeable items.
As lists are ordered, items can be called individually by referring to their index number.
Also, the items in the list can be manipulated and hence the list is said to be mutable.
In python, there are the following methods to find the length of the list:
1. Using the len() method
len()is a built-in method for getting the total number of items in a list. This method takes the list as an argument and returns the length of the provided list.
Syntax:
len(List_name)
Example:
# Python program to find length of list
# using len() method
# Initializing list to find length
MyList = ['India', 'USA', 'UK', 'Russia'];
print("Length of the list is: ", len(MyList))
Output:
Length of the list is: 4
Execution time: 0.0 (seconds)
2. Using the Naive method
Naive method is another basic method for finding the length of the list. In this method, first, we initialize the value of the counter to 0, then we use a for loop which increases the value of the counter by 1 for each item in the list.
Finally, we print the value of the counter which returns the length of the list.
Example:
# Python program to find length of list
# using naive method
# Initializing list to find length
MyList = ['India', 'USA', 'UK', 'Russia'];
# Initializing counter variable with 0
counter = 0
# Run for loop to calculate length of list
# By incrementing value of counter by 1
for x in MyList :
counter = counter + 1
print("Length of the list is: ", counter)
Output:
Length of the list is: 4
Execution time: 0.00057 (seconds)
3. Using the length_hint()method
The other technique to find the length of the list is the length_hint() method.
This method is defined in the operator class and to use this method we have to import length_hint from the operator class.
length_hint() takes the list as an argument as shown below and then returns the length of the list.
Syntax:
length_hint(list)
Example:
# Python program to find length of list
# using length_hint method
from operator import length_hint
# Initializing list to find length
MyList = ['India', 'USA', 'UK', 'Russia'];
print("Length of the list is: ", length_hint(MyList))
Output:
Length of the list is: 4
Execution time: 0.00046(seconds)
4. Using the __len__() method
__len__() method is another built-in method available in python for finding the length of the list.
Though len() and __len__() method looks similar there is the slightest of difference in these two.
When len() function is called it internally calls the __len__() method which returns the counts of items in the sequence i.e the list. Also, we can directly call the __len__() method as shown in the example below.
Example:
# Python program to find length of list
# using __len__() method
# Initializing list to find length
MyList = ['India', 'USA', 'UK', 'Russia'];
# Get length of list
length = MyList.__len__()
print("Length of the list is: ", length)
Output:
Length of the list is: 4
Execution time: 0.0 (seconds)
CONCLUSION:
Now, since we know four methods for finding the length of the list, the question arises which method is best among all the available ones?
The answer is quite simple, the method with minimum execution time is the best one, and on analyzing the time taken by each method we can conclude :
Execution time:
- Naive method: 0.00057 (seconds)
- length_hint() : 0.00046 (seconds)
- __len__() : 0.0 (seconds)
- len(): 0.0 (seconds)
Hence, len() is the best method that we could use since it has the least execution time among all the available methods.